MATLAB Answers

how to change order of legends?

740 ビュー (過去 30 日間)
Mr M.
Mr M. 2015 年 9 月 23 日
Commented: Walter Roberson 2019 年 8 月 19 日
I dont want to change the order of my plots, but I want to change the order in the legend list. How to do?

  0 件のコメント

サインイン to comment.

件の回答 (7)

David
David 2018 年 1 月 10 日
編集済み: David 2018 年 1 月 10 日
I think Mr. M means the following: In the figure GUI, after one has worked hard to generate a nice figure by combining the outputs of various scripts via copy + paste, how can one change the order of the legend?
In earlier versions of MATLAB, one could achieve this by cutting and repasting traces. They would then reappear at the bottom of the legend. So you would take the trace you want to be second, cut and paste it, then do the same for the third, all the way to the Nth, and then the legend would have the desired order. Now it is no longer possible to do this. The figure somehow remembers the position of the trace before it was cut.
But there is a WORK-AROUND! Simply cut the trace, paste it in onto a different axis of a different figure, then cut that trace and paste it back onto the original figure. This stymies matlab's attempt to remember the order of the trace and puts it onto the bottom of the legend.

  4 件のコメント

表示 1 件の古いコメント
Jeremy Cribb
Jeremy Cribb 2018 年 3 月 15 日
Hi, Navid. Your suggestion works for older versions of matlab, in my experience up to 2014b. I haven't used versions between R2014b and R2017b. David's strategy was required for me to re-order in v2017b.
Mr M.
Mr M. 2018 年 3 月 20 日
What is important for me is the layer-order of the curves. So I need a specific order, but another order in the legends.
Walter Roberson
Walter Roberson 2019 年 8 月 19 日
Use https://www.mathworks.com/help/matlab/ref/uistack.html

サインイン to comment.


Rick
Rick 2018 年 3 月 24 日
use this code to swap the order of two plots:
plots=get(gca, 'Children');
legend(plots(2, 1), {'Second line', 'First line'});

  1 件のコメント

Walter Roberson
Walter Roberson 2018 年 8 月 21 日
You probably mean
legend(plots([2, 1]), {'Second line', 'First line'});

サインイン to comment.


George
George 2015 年 9 月 23 日
Is this something you are after, the question is not clear
x=magic(2);
plot(x(:,1));
hold
plot(x(:,2),'r');
legend('Data1','Data2');
%blue = Data1
%red=Data2
to change that simple reverse the string order
legend('Data2','Data1');

  0 件のコメント

サインイン to comment.


sarmad a
sarmad a 2018 年 3 月 15 日
@David: you really saved my time !! Thanks

  0 件のコメント

サインイン to comment.


FM
FM 2018 年 5 月 22 日
There is a good solution in the `Update` section of this post on Stack Overflow: http://stackoverflow.com/a/39104494/3230708

  0 件のコメント

サインイン to comment.


Ed Rawi
Ed Rawi 2018 年 10 月 22 日
編集済み: Ed Rawi 2018 年 10 月 23 日
a = linspace(1,10,10);
b1 = sin(a);
Plot1 = plot(a,b1);
hold on
b2 = cos(a);
Plot2 = plot(a,b2);
hold off
legend([plot2 plot1],{Second,First})

  1 件のコメント

Jim Bosley
Jim Bosley 2019 年 8 月 19 日
Last line needs plot handles capitalized, as they were defined.
a = linspace(1,10,10);
b1 = sin(a);
Plot1 = plot(a,b1);
hold on
b2 = cos(a);
Plot2 = plot(a,b2);
hold off
legend([Plot2 Plot1],{Second,First})

サインイン to comment.


Liwei Wang
Liwei Wang 2019 年 1 月 25 日
Cut+paste is a good idea. But sometimes it won't work because the plots are pretty close to each other. An alternative approach is to change the order of the plots by code:
answered by the cyclist.
Here is the copy of the code:
figure
scatter(rand(150,1),rand(150,1))
hold on
fill([0.2 0.5 0.5 0.2],[0.2 0.2 0.5 0.5],'r')
hg = line([0 0.6],[0.6 0]);
set(hg,'LineWidth',12,'Color','g')
h = get(gca,'Children');
set(gca,'Children',[h(3) h(2) h(1)])
Refresh legend then you will see the change.

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by