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

2 views (last 30 days)
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 Comments
jaah navi
jaah navi on 8 Aug 2021
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.

Sign in to comment.

Accepted Answer

Chunru
Chunru on 8 Aug 2021
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 Comments
Chunru
Chunru on 9 Aug 2021
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)

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 9 Aug 2021
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 Comment
jaah navi
jaah navi on 9 Aug 2021
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.

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!