Using Legend

조회 수: 15 (최근 30일)
kate
kate 2011년 6월 5일
I have a 5 by 40 matrix and I’d like to plot 27 first columns as one color and last 13 columns as second color. My problem is that when i put legend to label each color, both legends are shown with the same color (always the same color as the first 27 columns). I appreciate your help. Thanks!

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 6월 5일
Look at the example in the document here. The legend should have the same color as they are used in the curve. What version of Matlab are you using?
  댓글 수: 1
kate
kate 2011년 6월 5일
I read this page several time, here is a simple code that shows the problem:
x=[1 2 3 4 5 6;7 8 9 10 11 12;13 14 15 16 17 18]
figure
hold on
time=[1,2,3]
p=plot(time, x(:,1:4), 'b',...
time, x(:,5:6), 'r');
set(p, 'Linewidth',2);
legend('\fontsize{13} a', '\fontsize{13} b', 'Location', 'Best');
When you run it, the labels have the same color.
Don’t know where my mistake is. I appreciate any help.
I'm using Matlab 7.11.0 (R2010b).

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

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2011년 6월 6일
In your example, if you have legend('a','b','c','d','e','f'), you'll have the right legend. The problem is that you have 6 curves, the first 4 are color blue and the last 2 are color red. You only made 2 legend marks thus it took the color of the first 2 curves (both are blue). Matlab legend() function is doing the right thing.
But, that is the problem you want to solve. Try this,
h=legend('a')
c=get(h,'Children')
set(c,'Color',[0 1 0])
You will see that it only makes one legend mark. Each legend mark will have three children (probably for line, marker and string). You can change the color for all of them by specifying the color as [Red Green Blue]. I believe this can solve your original problem.
  댓글 수: 3
Fangjun Jiang
Fangjun Jiang 2011년 6월 6일
h=legend('a','b');
c=get(h,'Children');
set(c(1:3),'Color',[0 1 0]);
set(c(4:6),'Color',[1 0 0]);
Try to match them.
kate
kate 2011년 6월 6일
It works now!
Thanks a million, you saved lots of time for me.
I really appreciate your help.

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

카테고리

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