Could anyone help me how to have the legend as in the desired manner for two y axis.

조회 수: 3 (최근 30일)
Could anyone help me how to write the legend with two y axis as in the desired format.
I have generated the line graph with two y axis , 5 lines for lefy y axis and 5 lines for right y axis so total 10 lines
Now I want to have the legend in a manner that left y axis should be dotted line with different marker and right y axis should be solid line with the marker similar to left y axis.
Could anyone please help me on this.
  댓글 수: 2
KSSV
KSSV 2021년 8월 8일
Show us your code. And first acknowledge the previous questions you have asked.
jaah navi
jaah navi 2021년 8월 8일
x=1:10
y1=rand(1,10)
y2=rand(1,10)
y3=rand(1,10)
y4=rand(1,10)
figure(1)
[hAX,hLine1,hLine2] = plotyy(x, [y1; y3], x, [y2; y4], @(X,Y)semilogy(X,Y), @(X,Y)plot(X,Y));
set(hLine1(1),'LineStyle','-','Marker','*', 'Color', 'r');
set(hLine1(2),'LineStyle','-','Marker','o', 'Color', 'b');
set(hLine2(1),'LineStyle','--','Marker','*', 'Color', 'r');
set(hLine2(2),'LineStyle','--','Marker','o', 'Color', 'b');
set(gca,'XTick',[1:1:10])
xlabel('persons')
ylabel(hAX(1),'weight') % left y-axis
ylabel(hAX(2),'height') % right y-axis
grid on;
lgd=columnlegend(2,{'Batch 1','Batch 2 ','Batch 1','Batch 2'})
when I run the code i am getting the legend in the graph as follows
But actually i want 'Batch 1','Batch 2 ' to be displayed once with markers alone followed by solid line mentioning weights and dotted line mentioning heights.
Could you please help me on this.

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

채택된 답변

Chunru
Chunru 2021년 8월 8일
Change the last line
x=1:10;
y1=rand(1,10);
y2=rand(1,10);
y3=rand(1,10);
y4=rand(1,10);
figure(1)
[hAX,hLine1,hLine2] = plotyy(x, [y1; y3], x, [y2; y4], @(X,Y)semilogy(X,Y), @(X,Y)plot(X,Y));
set(hLine1(1),'LineStyle','-','Marker','*', 'Color', 'r');
set(hLine1(2),'LineStyle','-','Marker','o', 'Color', 'b');
set(hLine2(1),'LineStyle','--','Marker','*', 'Color', 'r');
set(hLine2(2),'LineStyle','--','Marker','o', 'Color', 'b');
set(gca,'XTick',[1:1:10])
xlabel('persons')
ylabel(hAX(1),'weight') % left y-axis
ylabel(hAX(2),'height') % right y-axis
grid on;
lgd=legend({'Batch 1','Batch 2 '});
  댓글 수: 4
jaah navi
jaah navi 2021년 8월 9일
I want to have the legend as seen below. Could you please help me on this.
Chunru
Chunru 2021년 8월 9일
Though I don't recommend such manual adjustment, it can be done:
x=1:10;
y1=rand(1,10);
y2=rand(1,10);
y3=rand(1,10);
y4=rand(1,10);
figure(1)
[hAX,hLine1,hLine2] = plotyy(x, [y1; y3], x, [y2; y4], @(X,Y)semilogy(X,Y), @(X,Y)plot(X,Y));
set(hLine1(1),'LineStyle','-','Marker','*', 'Color', 'r');
set(hLine1(2),'LineStyle','-','Marker','o', 'Color', 'b');
set(hLine2(1),'LineStyle','--','Marker','*', 'Color', 'r');
set(hLine2(2),'LineStyle','--','Marker','o', 'Color', 'b');
set(gca,'XTick',[1:1:10])
xlabel('persons')
ylabel(hAX(1),'weight') % left y-axis
ylabel(hAX(2),'height') % right y-axis
grid on;
axes(hAX(2)); hold on
text(7, 0.2, '* Batch 1', 'FontSize', 8); text(8.5, 0.2, 'o Batch 2', 'FontSize', 8);
plot([7 8], [0.15 0.15], 'b-'); plot([7 8], [0.1 0.1], 'b--');
text(8.5, 0.15, 'height', 'FontSize', 8);
text(8.5, 0.1, 'weight', 'FontSize', 8)

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 8월 9일
th(1) = plot(nan,nan,'*', 'displayname', 'Batch 1');
th(2) = plot(nan,nan,'o', 'displayname', 'Batch 2');
th(3) = plot(nan,nan,'-', 'displayname', 'height');
th(4) = plot(nan,nan,'--', 'displayname', 'weight');
legend(th, 'show')
  댓글 수: 1
jaah navi
jaah navi 2021년 8월 9일
when I run the command i am getting error stating Error using legend>process_inputs
Invalid argument. Type 'help legend' for more information.
Could you please help me on this.

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by