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

Will picture Will · Jun 29, 2012 · Viewed 42.9k times · Source

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

Answer

Amro picture Amro · Jun 29, 2012

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