How to change color of lines on legend?

조회 수: 488 (최근 30일)
Mahdi Torabi
Mahdi Torabi 2018년 7월 14일
댓글: Walter Roberson 2020년 5월 1일
Is there any way to change the color of lines in the legend AFTER plotting the graph(like using Edit\axes properties)?
  댓글 수: 4
Walter Roberson
Walter Roberson 2018년 7월 14일
You could record the handle of the first of each type, and then pass that vector of handles as the first argument to legend().
Or you could do something like
L(1) = plot(nan, nan, 'b-');
L(2) = plot(nan, nan, 'r--');
legend(L, {'first case', 'second case'})
Mahdi Torabi
Mahdi Torabi 2018년 7월 14일
Thank you so much. This solved the problem.

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

채택된 답변

Mahdi Torabi
Mahdi Torabi 2018년 7월 14일
편집: Matt J 2018년 7월 14일
According to the Walter answer on above my problem solved easily using :
L(1) = plot(nan, nan, 'b-');
L(2) = plot(nan, nan, 'r--');
legend(L, {'first case', 'second case'})
  댓글 수: 2
Matt J
Matt J 2018년 7월 14일
This gives an error,
Error using legend>process_inputs (line 554)
Invalid argument. Type 'help legend' for more information.
Error in legend>make_legend (line 306)
[autoupdate,orient,location,position,children,listen,strings,propargs] = process_inputs(ha,argin); %#ok
Error in legend (line 259)
make_legend(ha,args(arg:end),version);
Walter Roberson
Walter Roberson 2018년 7월 15일
Turns out you just need
hold on
to be in effect, as otherwise the second plot() clears the axes.

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

추가 답변 (1개)

Matt J
Matt J 2018년 7월 14일
편집: Matt J 2018년 7월 14일
This might be what Walter meant,
t=linspace(0,1,10);
L(1:2) = plot(t,exp(-t), 'r--',nan,nan,'k--');hold on
L(3:4) = plot(t,1-exp(-t), 'g^-',nan,nan,'m^-'); hold off
legend(L([2,4]), 'Red','Green')
  댓글 수: 2
Luiz Bacelar
Luiz Bacelar 2020년 5월 1일
how to solve this? I have similar problem
Walter Roberson
Walter Roberson 2020년 5월 1일
plot(nan,nan) with appropriate line style and color, once for each different thing you want to appear in the legend, recording the handle that is returned. Then when you call legend(), pass that array of handles in as the first parameter to legend()

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

카테고리

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