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

2 次查看(过去 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 个评论
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 个评论
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