legend for multiple plots
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello,
I have 4 matrix with 6 rows and 15 columns. I want to plot all these matrix in on plot. for this aim I used the followin code:
x=1:15
plot(x,res540,'-r')
hold on
plot(x,res720,'--b');
plot(x,res900,'-.g');
plot(x,res1080,':k','LineWidth',0.2);
now I need to put a legend for above code in one plot. but each method I used for it does not work or all legend has same color and style. what should I do for this? Thanks
댓글 수: 0
채택된 답변
Voss
2022년 5월 31일
% some matrices:
res540 = rand(6,15);
res720 = 1+rand(6,15);
res900 = 2+rand(6,15);
res1080 = 3+rand(6,15);
% store the line handles returned from plot;
% each plot call returns 6 lines
h = zeros(6,4);
x=1:15;
h(:,1) = plot(x,res540,'-r');
hold on
h(:,2) = plot(x,res720,'--b');
h(:,3) = plot(x,res900,'-.g');
h(:,4) = plot(x,res1080,':k','LineWidth',0.2);
% make a legend for the 1st line returned from
% each plot call (i.e., first row of h)
legend(h(1,:),{'540' '720' '900' '1080'})
추가 답변 (1개)
Bjorn Gustavsson
2022년 5월 31일
Use the outputs from plot to explicitly control what is show in the legend:
x=1:15
ph1 = plot(x,res540,'-r');
hold on
ph2 = plot(x,res720,'--b');
ph3 = plot(x,res900,'-.g');
ph4 = plot(x,res1080,':k','LineWidth',0.2);
legend([ph1(1),ph2(1),ph3(1),ph4(1)],'t1','t2','t3','t4')
HTH
댓글 수: 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!