필터 지우기
필터 지우기

Can MATLAB legend color order be changed as the legend element order changes?

조회 수: 8 (최근 30일)
Using the following code from the 'Specify Legend Descriptions During Line Creation' example in MATLAB help, I've created the following plot:
I've successfully changed the order of the legends entries using the following steps:
1. Clicked the arrow button so I could select the legend
2. Double clicked on the legend
3. Selected More Properties from the property editor
4. Selected the 'String' property and re-ordered the elements of the legend
The order of the legend elements did change. However, the associated color scheme did not.
Is there a separate function for this? Can the color order be changed as well?
Thanks.

채택된 답변

Mike Garrity
Mike Garrity 2015년 6월 16일
I got lost at this step:
4. Selected the 'String' property and re-ordered the elements of the legend
I don't think that there's anything in the editor for the String property which lets you re-order the elements of the legend.
Perhaps you just changed the edited the strings without changing the order? To actually change the order of the entries, you probably want to do something like this:
legend([p2 p3 p1])
or even
p = [p1 p2 p3];
for i=1:100
legend(p(randperm(3)))
drawnow
end

추가 답변 (2개)

Joseph Cheng
Joseph Cheng 2015년 6월 15일
I'll start off with Yes it can be done, and No there is not a separate function. What you did by double clicking is not changing the assignment but just the displayed text. There is some trickery to change the color, positioning, text, etc of a legend I would say it is much easier to just follow the documentation on legend than dig into its parameters. Plot the lines in the order you want to display it in the legend and label the lines using legend() appropriately.
  댓글 수: 2
Brad
Brad 2015년 6월 16일
But how does that account for the uncertainty of the element order? Is there no way to consistently associate a specific color to a legend element?
Joseph Cheng
Joseph Cheng 2015년 6월 16일
legend element is tied to the displayed line (color, marker type, line style, etc.). If they are not and are mixed up I would say there is 99% your inputs to the legend function is probably in the wrong order.

댓글을 달려면 로그인하십시오.


Brad
Brad 2015년 6월 18일
Mike, you are correct. When changing the order of the strings, the colors didn't change. But when I changed the legend entries via legend([p2 p3 p1]), the colors changed with them. Good catch! Thanks.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by