How to manually arrange the Legend strings
조회 수: 15 (최근 30일)
이전 댓글 표시
I have plot in which it consists of 9 lines.I want to describe all these lines step by step for example it should display the red line first with 3 variable case and similarly for remaining two colors.
댓글 수: 0
답변 (2개)
Brendan Hamm
2015년 8월 10일
With 3 variable case, what does this mean? I'm going out on a whim and assuming you just want to change the order of the lines.
By default the order of the lines in the legend is the same as they are plotted. The easiest way is to just plot them in the order you want the legend to appear.
If that is not feasible, then you can change the order of the lines as they are stored in the 'Children' property of the axes. This defines the order they are listed in the legend (except they are flipped so that the first element in the list is the last legend entry).
% Make some data to plot
x = 0:0.01:1;
y = randn(3,length(x));
ax = axes;
hold all % Allows color to be cycled (even prior to 2014b)
% Plot the data
for k = 1:3
plot(x,y(k,:))
end
legend('A','B','C')
% In 2015a this is blue, red, yellow
myLines = get(ax,'Children');
% Change the order the data is plotted.
set(ax,'Children',myLines([3 1 2]));
legend('A','B','C')
% Now it is red, blue , yellow
댓글 수: 2
Arthur Vieira
2021년 6월 16일
편집: Arthur Vieira
2021년 6월 16일
But that switches the order the plotted lines. I need to switch the order of the legends, but keep the same plot order. In my case I'm plotting two fill()s and the two plot()s. The fills represent the standard deviation and the plots the mean. So naturally the plots need to be on top of the fills [fill1 fill2 plot1 plot2], but the legends should be ordered [plot1 fill1 plot2 fill2] in order to make sense. Is there really no way but to photoshop it later? xD
Arthur Vieira
2021년 6월 16일
Found the solution here:
https://se.mathworks.com/matlabcentral/answers/244707-how-to-change-order-of-legends
Govind K
2015년 8월 10일
댓글 수: 1
Marya Sadki
2021년 11월 29일
Hi, are you find a solution for this?
If yes, please tell me i have the same problem
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!