Legend repeating same color
조회 수: 64 (최근 30일)
이전 댓글 표시
Please I am just plotting two data extracted from my work. but after plotting the code, the color of the legend keep repeating with the first color I input with indicating the legend of the next color. The attached data, Dposition and Uposition are the data plotted. please I hope anyone can help.
댓글 수: 1
Adam
2019년 9월 11일
It's impossible to say much without you showing your code. The legend does what your code tells it to, but we don't know what that is. Also, note that the legend simply takes the colours from the plots that it is asked to include. You do have 2 blue plots on that graph so presumably it is showing those two, but again, we don't really know as we can't see any code.
채택된 답변
Rik
2019년 9월 11일
편집: Rik
2019년 9월 11일
Set the DisplayName property when calling plot, or use the output from plot to build your legend with specific handles.
x=linspace(0,10,50);
y1=5*cos(x)+rand(size(x));
y2=4*sin(x)+rand(size(x));
y1a=y1+0.5*rand(size(x));
y1b=y1-0.5*rand(size(x));
figure(1),clf(1)%only use clf during debugging
%example 1:
subplot(1,3,1)
plot(x,y1,'DisplayName','foo'),hold on
plot(x,y2,'DisplayName','bar'),hold off
legend
%example 2:
subplot(1,3,2)
h1=plot(x,y1);hold on
h2=plot(x,y2);hold off
legend([h1 h2],{'foo','bar'})
%example 3:
subplot(1,3,3)
h1=plot([x;x]',[y1a;y1b]');hold on
h2=plot(x,y2);hold off
legend([h1(1) h2(1)],{'foo','bar'})
댓글 수: 1
Chaman Dewangan
2020년 12월 19일
I also get same legend colour for two different plots. Example one is helpful. It solves the error. Thanks
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!