How do i display the legend of multiple lines in a figure?
2 views (last 30 days)
Show older comments
I am having an issue with displaying legend entries for multiple lines in a figure. I followed an example on here overlapping axes and modifying tick marks. However, the example never displayed a legend. Currently Matlab displays a figure such as below, I need to link the correct line to the corresponding label. Any help is appreciated, thank you. If possible I would also like to know how to move axes out a bit from the plot horizontally. Code for figure is below image:
display('Plotting');
x = 3300:3800;
y1 = RPM; y2 = VS; y3 = PT; y4 = FDT; y5 = TCT; y6 = RPS; y7 = Energy;
figure;
ax1 = gca; get(ax1,'Position') set(ax1,'Xcolor','k','YColor','r','YLim',[0,2000],'YTick',[0:200:2000]); line(t(x),y1(x),'Color','r','Marker','.','Parent',ax1)
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','bottom',... 'YAxisLocation','right','Color','none','XColor','b','YColor','b',... 'YLim',[-32.5,-25],'YTick',[-32.5:2.5:-25],'XTick',[],'XTickLabel',[]); line(t(x),y4(x),'Color','b','Marker','none','Parent',ax2)
ax3 = axes('Position',get(ax1,'Position'),'XAxisLocation','bottom',... 'YAxisLocation','right','Color','none','XColor','b','YColor','b',... 'YLim',[-32.5,-25],'YTick',[-32.5:2.5:-25],'XTick',[],'XTickLabel',[]); line(t(x),y5(x),'Color','b','Linestyle','--','Marker','none','Parent',ax3)
ax4 = axes('Position',get(ax1,'Position'),'XAxisLocation','bottom',... 'YAxisLocation','right','Color','none','XColor','b','YColor','c',... 'YLim',[0,280],'YTick',[40:20:140],'XTick',[],'XTickLabel',[]); line(t(x),y6(x),'Color','c','Marker','none','Parent',ax4)
ax5 = axes('Position',get(ax1,'Position'),'XAxisLocation','bottom',... 'YAxisLocation','right','Color','none','XColor','b','YColor','m',... 'YLim',[0,280],'YTick',[40:20:140],'XTick',[],'XTickLabel',[]); line(t(x),y2(x),'Color','m','Marker','none','Parent',ax5)
ax6 = axes('Position',get(ax1,'Position'),'XAxisLocation','bottom',... 'YAxisLocation','right','Color','none','XColor','b','YColor','m',... 'YLim',[0,280],'YTick',[40:20:140],'XTick',[],'XTickLabel',[]); line(t(x),y7(x),'Color','g','Marker','none','Parent',ax5)
ax7 = axes('Position',get(ax1,'Position'),'XAxisLocation','bottom',... 'YAxisLocation','right','Color','none','XColor','b','YColor','m',... 'YLim',[0,280],'YTick',[40:20:140],'XTick',[],'XTickLabel',[]); line(t(x),y3(x),'Color','k','Marker','none','Parent',ax5)
legend([ax2;ax3;ax4;ax5;ax6;ax7])
0 Comments
Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!