11

Consider following code:

t=0:.01:(2*pi);
y=[sin(t);sin(t-pi/12);sin(t-pi/6);sin(t-pi/4)];
figure(1)
    clf
    subplot(6,1,5)
    plot(t,y)
    xlim([0 2*pi])
    legend('1','2','3','4')

It produces following figure:

![enter image description here

Is there a way to change the legend to a 2-column lay-out? So it would be

--- 1 --- 3

--- 2 --- 4

instead of

--- 1

--- 2

--- 3

--- 4

so the legend boundary lined would not cross the graph boundary lines.

I found the gridLegend script, but I prefer to code it directly.

Community
  • 1
  • 1
Karlo
  • 1,630
  • 4
  • 33
  • 57
  • 4
    Not sure if you can with orthodox methods but really interesting question. Probably someone can hack into java and do it. Probably `gridLegend` is best – Ander Biguri Jul 25 '16 at 14:16
  • 1
    If you want to code it directly then follow along with what `gridLegend` is doing and implement it yourself. – sco1 Jul 25 '16 at 14:29
  • @excaza Yes, that was also my first idea. But it seems that it is not as easy as adding 1 or 2 lines of code to a `legend` statement. – Karlo Jul 25 '16 at 14:38
  • 3
    I would advise to simply use `gridLegend`. There's really no point re-inventing the wheel. – nirvana-msu Jul 25 '16 at 19:02
  • 2
    What about `legend('1','2','3','4','Orientation','horizontal')` so the legend doesn't cross the border of the axes? This might not solve your *question* but could be a straight-forward solution to your *problem*. – Matt Jul 25 '16 at 20:30
  • @Matt Indeed, that's an interesting alternative I hadn't thought of. – Karlo Jul 26 '16 at 08:16
  • 1
    Other than gridlegend there's also columnlegend: http://www.mathworks.com/matlabcentral/fileexchange/27389-columnlegend – optimist Jul 26 '16 at 09:38
  • @Karlo, I have given a direct solution to your question. – ShadowWarrior Oct 17 '18 at 14:04

2 Answers2

6

MATLAB has introduced native support for multiple columns in legend from version 2018a. Just add 'NumColumns',desired_number at the end of the legend() command.

See details here - https://www.mathworks.com/help/matlab/ref/legend.html?lang=en&s_tid=gn_loc_drop#bt6r30y

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. To order the items from left to right along each row instead, set the Orientation property to 'horizontal'.

ShadowWarrior
  • 180
  • 1
  • 12
1

You can usually hack this sort of thing by making a second invisible axis on top of the first, like this:

t=0:.01:(2*pi);
y=[sin(t);sin(t-pi/12);sin(t-pi/6);sin(t-pi/4)];
figure
subplot(6,1,5)

plot(t,y)
xlim([0 2*pi])
l1 = legend('1', '2');
pos = l1.Position;
set(l1, 'Position', pos - [pos(3) 0 0 0]);
legend boxoff

ax2 = copyobj(gca, gcf);
set(ax2, 'visible', 'off', 'clipping', 'off')
kids = ax2.Children;
set(kids, 'visible', 'off', 'clipping', 'off')
set(ax2, 'children', kids([3:4 1:2]))
l2 = legend(ax2, '3', '4');
legend(ax2, 'boxoff')
legend boxoff

Note that this is fragile (e.g., doesn't handle the window being resized on my version of MATLAB).

Tokkot
  • 1,215
  • 7
  • 22
  • Interesting hack. Could indeed be improved so the two legends are at the same height for each figure window size. – Karlo Aug 15 '16 at 08:39