How do I add two legends to a single plot in MATLAB?
Asked Answered
O

4

9

I'd like to add two legends to a plot in MATLAB. How can I do this?

Officiant answered 28/6, 2012 at 22:37 Comment(0)
C
11

You could create a second superimposed axis, with a legend of its own (in a different location of course).


EDIT:

Here is a quick example:

%# create some plot with a legend
hAx(1) = axes();
hLine(1) = plot(1:10, 'Parent',hAx(1));
set(hAx(1), 'Box','off')
legend(hLine(1), 'line')

%# copy the axis
hAx(2) = copyobj(hAx(1),gcf);
delete( get(hAx(2),'Children') )            %# delete its children
hLine(2) = plot(sin(1:10), 'Color','r', 'Parent',hAx(2));
set(hAx(2), 'Color','none', 'XTick',[], ...
    'YAxisLocation','right', 'Box','off')   %# make it transparent
legend(hLine(2), {'curve'}, 'Location','NorthWest', 'Color','w')

screenshot

Cuspid answered 29/6, 2012 at 0:54 Comment(0)
U
5

To create a sticky legend, you can call copyobj

handle_legend = legend(handle_plot, 'string1');
copyobj(handle_legend, handle_figure);

The copyobj function simply retain its associated legend within the figure.

This works within a single axes (no need to create a second superimposed axes), and several legends can be added this way.

Example:

%declare figure
hfigure = figure('Color', 'w');

%plot 2 lines (red and blue)
hplot1 = plot(1:10,'r-.x');
hold on;
hplot2 = plot(10:-1:1,'b--o');

%plot legends
hlegend1 = legend(hplot1, 'Data 1', 'Location','East'); %display legend 1
new_handle = copyobj(hlegend1,hfigure);                 %copy legend 1 --> retain
legend(hplot2, 'Data 2', 'Location','West');            %display legend 2

enter image description here

Unequal answered 24/4, 2014 at 0:17 Comment(1)
This method does not work with me (R2016a), not even when using 'legacy' as an option.Darcidarcia
D
1

After you made the first legend, make a new, invisible axis handle:

ax=axes('Position',get(gca,'Position'),'Visible','Off');

Now make the second legend in the new axis:

legend(ax,...);

It's principly the same as @Amro's answer, but simpler and shorter.

Darcidarcia answered 12/5, 2016 at 21:18 Comment(1)
I tried this in R2016b without success: Plot1; adjust xticks and yticks; new axes; ...; The new ticks and labels overlap with the old ones.Garibay
F
1

example for multiple plots :

hAx(1) = axes();
hold on
hLine(1) = plot(1:10, 'Parent',hAx(1),'color','b');
hLine(2) = plot(3:15, 'Parent',hAx(1),'color','b', 'linestyle','--');
set(hAx(1), 'Box','off')
legend([hLine(1), hLine(2)],{ 'line' 'line2'})

%# copy the axis
hAx(2) = copyobj(hAx(1),gcf);
delete( get(hAx(2),'Children') )            %# delete its children
hold on
hLine(3) = plot(sin(1:10), 'Color','r','Parent',hAx(2));
hLine(4) = plot(cos(1:10), 'Color','r','linestyle','--','Parent',hAx(2));
hold off
set(hAx(2), 'Color','none', 'XTick',[], ...
'YAxisLocation','right', 'Box','off')   %# make it transparent
legend([hLine(3),hLine(4)], {'sin' , 'cos'}, 'Location','NorthWest', 'Color','w')
%legend(hLine(3), {'sin'}, 'Location','NorthWest', 'Color','w')
Fascism answered 15/2, 2017 at 8:55 Comment(1)
totally nonobvious, but this was what I needed in octave, thx: legend([hLine(3),hLine(4)], {'sin' , 'cos'})Jargonize

© 2022 - 2024 — McMap. All rights reserved.