how to use the legend command to display the label of the samples in a plot

3 visualizaciones (últimos 30 días)
I have written a 2 functions, in which each displays a plot graph at the end...
now i want to display both graph in same plot.... by calling each function one after other and entering "hold all" , i'm getting the graph i wanted.... now the problem is the legend information is being displayed only the last function graph.... can anyone solve this problem

Respuesta aceptada

Friedrich
Friedrich el 29 de Abr. de 2014
Editada: Friedrich el 29 de Abr. de 2014
Hi,
you have to call legend once so that it displays the information of both functions. So for now you do roughly that
plot(1:10);
legend('a')
hold all
plot(1:10,sin(1:10))
legend('b')
hold off
But you need to call it like this
plot(1:10);
hold all
plot(1:10,sin(1:10))
legend('a','b')
hold off
Or in the case you don't know the legend tags you can use the existing legend and recreate a new one using the old legend tags
plot(1:10);
legend('a')
hold all
plot(1:10,sin(1:10))
legend([get(legend,'String'),{'b'}])
hold off
  2 comentarios
Image Analyst
Image Analyst el 29 de Abr. de 2014
Sometimes the legends are all the same color and marker. Can you explain why? To Illustrate:
% Reference discussion:
% http://www.mathworks.com/matlabcentral/answers/126309#answer_133852
% Plot red asterisks.
xTest = rand(2,3);
yTest = rand(2,3);
plot(xTest, yTest, 'r*-', 'MarkerSize', 8, 'LineWidth', 2);
hold on;
% Plot blue circles.
xTrue = rand(4, 2); % As might come from meshgrid.
yTrue = rand(4, 2);
plot(xTrue, yTrue, 'bo-', 'MarkerSize', 8, 'LineWidth', 4);
% For some reason, both lengends show up as red crosses.
legend({'* = Test'; 'o = True'}); % Legend not working - don't know why.
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Friedrich
Friedrich el 30 de Abr. de 2014
Editada: Friedrich el 30 de Abr. de 2014
Seems to be a feature in legend in the way it gets the colors of the children of the axes. Basicall the red plot generates 3 children and the blue plot 2 children. Which makes a total of 5 children for the axes. When calling legend with two inputs the command seems ot look up the first two children in order to get the color which happens to be the children from the same plot which are red. What helps is to point the legend to the correct children to use, e.g.
xTest = rand(2,3);
yTest = rand(2,3);
h(1:3) = plot(xTest, yTest, 'r*-', 'MarkerSize', 8, 'LineWidth', 2);
hold on;
% Plot blue circles.
xTrue = rand(4, 2); % As might come from meshgrid.
yTrue = rand(4, 2);
h(4:5) = plot(xTrue, yTrue, 'bo-', 'MarkerSize', 8, 'LineWidth', 4);
legend([h(1),h(end)],{'* = Test'; 'o = True'});
If you would call
legend('1','2','3','4','5')
at the end you will see what I mean. I think thats the way its supposed to work because according to the doc
legend('string1','string2',...) displays a legend in the current axes using the specified strings to label each set of data.
And your plot commands generate 5 sets of data.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by