how to use the legend command to display the label of the samples in a plot
조회 수: 3 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Friedrich
2014년 4월 29일
편집: Friedrich
2014년 4월 29일
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
Image Analyst
2014년 4월 29일
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
2014년 4월 30일
편집: Friedrich
2014년 4월 30일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!