MATLAB Answers

Legend line spacing in R2014b

69 views (last 30 days)
Scott Webster
Scott Webster on 26 Nov 2014
Commented: Scott Webster on 22 Apr 2016
In previous versions I was able to control (to some degree) the line spacing in figure legends by changing the PlotBoxAspectRatio property of the legend. I can't seem to do this in R2014b. Anyone know of a new way of doing this?
Basically I can't figure out how to make the legend smaller than some minimum that Matlab seems to determine automatically, and I disagree with how small looks reasonable.


Sign in to comment.

Answers (2)

Judi on 21 Apr 2016
Edited: Judi on 21 Apr 2016
In R2015b, I found a hack around this annoying limitation. There appears to be a minimum fontsize that can't be overridden; however, entering a fontsize less than this minimum has the effect of reducing the line-spacing between the legend entries. This solution would only work if you were happy to have the fontsize at the inbuilt minimum. I'm delighted to find this works for my current graph!
A code snippet for my graph is as follows - your mileage may vary:
[legh3,legicons3,legplots3,legs3]=legend([sdplot1(1) sdplot2(1) sdplot3(1)],'Foo','Bar','Baz','Location','southeast');
legh3.Position=[ 0.81 0.35 0.1286 0.001]
Note that I'm also using the panel add-in, and there may be some interdependencies going on with how font size is treated.

  1 Comment

Scott Webster
Scott Webster on 22 Apr 2016
Unfortunately I can't duplicate this behaviour, I just get a really small legend font size. So it's probably something to do with panel (didn't try that)?

Sign in to comment.

Doug Hull
Doug Hull on 26 Nov 2014
Here are a variety of changes to legend:
The low level access you had to legend in the past becuase it was an axis is not there anymore. Can you show what you had in the past and what it looks like now so we have a better idea of how to help?


Scott Webster
Scott Webster on 26 Nov 2014
Thanks for the reply. Here is a minimal working example from R2013a:
x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);
set(leg,'PlotBoxAspectRatio',[1 0.25 1]);
This results in a figure like:
If you comment out the last line of code then you get this:
In this case you wouldn't want the smaller legend, but the example demonstrates control over the legend size. In R2014b, the code above does not work, because the legend is no longer an axis.
The most obvious place (to me) to try to control the legend size is by setting the 'Position' property, which takes [left,bottom,width,height], but you can't make the legend as small as before (maybe you can't even make it smaller than the default?).
So in R2014b, if you replace the last line of the above code with:
(note the very small height value) Then you get the following:
The legend is actually a bit smaller than the previous default, but you can't squash it down nearly as much as before, which is desirable in certain situations.
As a side note, the legend seems to move around if you specify a "too small" height or width parameter, so in this case the result reported by:
0.7247 0.7710 0.1714 0.0814
The "bottom" value is not what I specified. If I increase the height then then the legend goes back to being in the correct place.
Thanks for any further advice.
Chad Greene
Chad Greene on 26 Nov 2014
I noticed this same behavior when you posted your question--
leg.Position(3) = .2*leg.Position(3)
should adjust the width of the legend, but instead it tinkers with the x position. And setting leg.Position(4) too small similarly moves the legend vertically without resizing it.
Scott Webster
Scott Webster on 15 Dec 2014
I submitted a bug report regarding the changing legend position behaviour discussed as a subtopic here and got the following response back:
Thank you for reporting this issue. The behavior you have described is expected, and is due to how MATLAB “legend” handles a height or width value that is too small. For future reference, I have included the minimum dimensions for “legend” below.
  • minimum legend height is 0.1165 normalized units
  • minimum legend width is 0.1536 normalized units
I can understand why this behavior might be surprising, and I have conveyed your concerns to our developers to be considered for a future enhancement.
I asked a follow up question and it was confirmed that in addition to the "absolute minimum" sizes above, there are minimum sizes that depend on the legend contents.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by