Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 298
Will Avatar asked Jun 28 '12 22:06

Will


People also ask

Can you have two legends on MATLAB plot?

As far as I know, you can only have one legend-window for one set of axes in MATLAB, so the idea is: add a second (exatly equal) set of axes to the figure. make this axes invisible, so you don't see it later in the plot.

How do you make a two column legend in MATLAB?

Just add 'NumColumns',desired_number at the end of the legend() command. Additionally, the orientation of the legend entries can be changed from top-to-bottom to left-to-right. By default, the legend orders the items from top to bottom along each column.

How do you plot a legend position in MATLAB?

legend(___,'Location', lcn ) sets the legend location. For example, 'Location','northeast' positions the legend in the upper right corner of the axes. Specify the location after other input arguments. legend(___,'Orientation', ornt ) , where ornt is 'horizontal' , displays the legend items side-by-side.


1 Answers

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.

like image 53
tvo Avatar answered Sep 25 '22 01:09

tvo