I'd like to add two legends to a plot in MATLAB. How can I do this?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With