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 个评论
采纳的回答
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
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
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')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!