Legend line are the same always

조회 수: 4 (최근 30일)
Niki
Niki 2013년 10월 9일
댓글: Sean de Wolski 2013년 10월 10일
I'm using hold on to draw different figures and after I have all the figures I use legend('lable1','lable2','lable3') however, all the legend are used with the same color, do you know how to use the exact color for each legend ?
I searched through different post and none of them could solve my problem
figure; hold on
plot ([1:10]',rand(10),'r')
plot ([1:10]',rand(10),'b')
plot ([1:10]',rand(10),'c')
  댓글 수: 3
Niki
Niki 2013년 10월 10일
Now it should run, but I still could not solve the problem
Sean de Wolski
Sean de Wolski 2013년 10월 10일
Did you try the code I posted below that does exactly what you want?

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

답변 (2개)

sixwwwwww
sixwwwwww 2013년 10월 9일
편집: sixwwwwww 2013년 10월 9일
For matrices you can do like this
a = rand(10);
b = rand(20);
c = rand(30);
plot(a), hold on
plot(b), hold on
plot(c), hold on
legends = {'a', 'b', 'c'};
Legend=legend(legends);
set(Legend,'Interpreter','none');
In case of unknown number of matrices. you can use cell array to store legend text and then follow last two lines of code. I hope this will help
  댓글 수: 2
Niki
Niki 2013년 10월 9일
does not work , it is already mentioned and it is good when you want to plot a vector, but I am plotting matrix and I have three or more
Niki
Niki 2013년 10월 9일
I just added an example of three plots above

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


Sean de Wolski
Sean de Wolski 2013년 10월 9일
편집: Sean de Wolski 2013년 10월 9일
Pass the plot handles into legend
More
When you plot rand(10) it will plot 10 lines and give you back 10 handles. Thus you'll need to only pass one of these into legend for each color:
figure; hold on
h = plot ([1:10]',rand(10),'r'); %10 line handles
h = vertcat(h,plot([1:10]',rand(10),'b'); %add second 10 line handles
h = vertcat(h,plot([1:10]',rand(10),'c')); %add third 10 line handles
legend(h(1:10:30),'One','Two','Three'); %pass every 10th line handle into legend
  댓글 수: 1
Niki
Niki 2013년 10월 9일
does not work the color of legend lines are the same

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

카테고리

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