Legend of graph doesn't match plot color and style

조회 수: 11 (최근 30일)
Matt
Matt 2018년 10월 19일
댓글: Chris 2018년 10월 19일
Hello,
I have searched the forum and found some info regarding the legend color not matching the lines. However, I cannot get my head around the problem in my case where I have not only different colors, but also different line styles. Here is my code:
inputNumber=3;
figure
hold on;
plot(temps(1:25001,1,1),tempFrFilt(1:25001,1,1),'-b','linewidth',2);
plot(temps(1:25001,2,1),tempFrFilt(1:25001,2,1),'-.b','linewidth',2);
plot(temps(1:25001,3,1),tempFrFilt(1:25001,3,1),'--b','linewidth',2);
plot(temps(1:25001,1,1),tempChFilt(1:25001,1,1),'-b','linewidth',2);
plot(temps(1:25001,2,1),tempChFilt(1:25001,2,1),'-.b','linewidth',2);
plot(temps(1:25001,3,1),tempChFilt(1:25001,3,1),'--b','linewidth',2);
if inputNumber>1
plot(temps(1:25001,1,2),tempFrFilt(1:25001,1,2),'-g','linewidth',2);
plot(temps(1:25001,2,2),tempFrFilt(1:25001,2,2),'-.g','linewidth',2);
plot(temps(1:25001,3,2),tempFrFilt(1:25001,3,2),'--g','linewidth',2);
plot(temps(1:25001,1,2),tempChFilt(1:25001,1,2),'-g','linewidth',2);
plot(temps(1:25001,2,2),tempChFilt(1:25001,2,2),'-.g','linewidth',2);
plot(temps(1:25001,3,2),tempChFilt(1:25001,3,2),'--g','linewidth',2);
if inputNumber==3
plot(temps(1:25001,1,3),tempFrFilt(1:25001,1,3),'-y','linewidth',2);
plot(temps(1:25001,2,3),tempFrFilt(1:25001,2,3),'-.y','linewidth',2);
plot(temps(1:25001,3,3),tempFrFilt(1:25001,3,3),'--y','linewidth',2);
plot(temps(1:25001,1,3),tempChFilt(1:25001,1,3),'-y','linewidth',2);
plot(temps(1:25001,2,3),tempChFilt(1:25001,2,3),'-.y','linewidth',2);
plot(temps(1:25001,3,3),tempChFilt(1:25001,3,3),'--y','linewidth',2);
end
end
legend(cellstr(legendNames),'Location','southwest');
A picture can be found here:
What am I doing wrong?

채택된 답변

Chris
Chris 2018년 10월 19일
The legend adds entries for each plot in the order that they were added to the current axes. If you want to change the order, or skip plots, you can do something like this:
P1 = plot( ... );
P2 = plot( ... );
P3 = plot( ... );
legend([P3, P1],'a label','another label')
The above example creates a legend for the third and first plot.
  댓글 수: 6
Matt
Matt 2018년 10월 19일
Thank you Chris, it did the trick! I've got it, but its a tricky one from Matlab.
Chris
Chris 2018년 10월 19일
Cheers, glad I could help!

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

추가 답변 (0개)

카테고리

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