plotyy legend for multiple lines ( R2012a)

조회 수: 2 (최근 30일)
christian robledo
christian robledo 2017년 7월 29일
댓글: Star Strider 2017년 7월 29일
Hello ,
I am trying to plot a legend for multiple graphs in the plotyy mode. I have seen solutions like this:
legend([H1;H2],'Susceptibles','Infected','Concentration');
But how can i do it for every single line h(3)-h(9) ?
Thanks a lot
% Bed profile and velocities
subplot(2,1,1)
[ax,h1,h2]=plotyy(x6,y6,x1,y1)
h(3) = line(x7,y7, 'Parent', ax(1));
h(4) = line(x8,y8, 'Parent', ax(1));
h(5) = line(x9,y9, 'Parent', ax(1));
h(6) = line(x18,y18, 'Parent', ax(2));
h(7) = line(x19,y19, 'Parent', ax(2));
h(8) = line(x20,y20, 'Parent', ax(2));
h(9) = line(x21,y21, 'Parent', ax(2));
set(ax(1),'ytick',linspace(min(y6),max(y6),8));
set(ax(2),'ytick',linspace(min(y1),max(y1),3));
ylim(ax(1),([0 2.5]))
ylim(ax(2),([0 0.35]))
ylabel(ax(1),'u [m/s]')
xlabel('x [m]');
ylabel(ax(2),'y [m]')

채택된 답변

Star Strider
Star Strider 2017년 7월 29일
I no longer have access to R2012a, so I tested this in R2017a.
See if something like this works:
x = linspace(0, 2*pi,500); % Create Data
y = sin([1:6]'*x); % Create Data
figure(1)
[ax,h(1),h(2)]=plotyy(x,y(6,:),x,y(1,:)*2);
h(3) = line(x, y(3,:), 'Parent',ax(1));
h(4) = line(x, y(4,:)*1.5, 'Parent',ax(2));
legend(h, 'Line 6', 'Line 1', 'Line 3', 'Line 4')
Since you are storing the line handles in an array, just use the array itself as the argument to legend.
  댓글 수: 2
christian robledo
christian robledo 2017년 7월 29일
Hello, thanks for your answer, yes indeed it worked almost like you said . I just have to write everything inside .. thanks for your hint
legend([h1,h2,h1,h(3),h(4),h(5)],'a','b','c','a','b','c')
Star Strider
Star Strider 2017년 7월 29일
As always, my pleasure.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Two y-axis에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by