Change figure legend horizontal spacing

87 views (last 30 days)
Matthew
Matthew on 8 Jan 2019
Edited: Karl_469 on 17 Nov 2022
example legend.PNG
In the attached example legend, there's too much whitespace between the first marker (red) and the first legend entry (gefeuert). There's not enough whitespace between the first legend entry and the second marker.
How do I change these two things:
  1. Space between a marker and a marker entry
  2. Space between the first legend entry and the second marker
Thanks!
  1 Comment
Shubham Gupta
Shubham Gupta on 9 Jan 2019
About your 2nd point, I believe increasing width of the legend should increase the spacing between the 2 entries.
I am not sure about the 1st point though, I hope it helps in someway. Cheers

Sign in to comment.

Answers (2)

nbrake
nbrake on 24 Oct 2019
Edited: nbrake on 24 Oct 2019
L = legend;
I believe the property you are looking for is:
L.ItemTokenSize
^ This is a 2-vector. The first element specifies the distance between the legend symbol and the text. If you have errorbars, the second element specifies the height of those errorbars in the legend. I believe the default is [30,30]. If you want to reduce the distance between the marker and text:
L.ItemTokenSize(1) = 10;
  1 Comment
Matthew
Matthew on 26 Oct 2019
Thanks! This certainly fixes 'half' of it, in other words, this fixes the spacing between the marker and the text.
Is there a way to space the two entries further apart, as well?

Sign in to comment.


dpb
dpb on 9 Jan 2019
  1. Something legend gives no control of directly
  2. Increase the width of the legend will spread them out
hLg=legend('gefeuert','nicth gefeuert','Orientation','horiz');
pos=hLg.Position; % retrieve existing position
pos(3)=1.5*pos(3); % increase width value 50% in position 4-vector
hLg.Position=pos; % set new position
Beyond that, the legend object is almost totally opaque to the end user, unfortunately.
  1 Comment
Karl_469
Karl_469 on 17 Nov 2022
Edited: Karl_469 on 17 Nov 2022
By changing the position, I can only increase the spacing.
Unfortunatley, I want to reduce the spacing in which case my position Input is just ignored.
Has there been updates on this problem since 2019?
How to push this to Mathworks to be changed?
It is pretty basic and intuitive to have control over the legend, yet, Matlab causes troubles over and over. Just a few examples where you have to find often ridiculous solutions that usually cause some subsequent issues:
1) in matlab, it makes a difference whether to use plot or scatter regarding the legend, because with scatter you cannot directly control the legend(!) marker icon size (https://ch.mathworks.com/matlabcentral/answers/50250-how-can-i-change-the-marker-size-in-legend)
2) you cannot directly chose the objects to be displayed in the legend (obviously, Mathworks cannot think of a case where you might select just a few objects, not all, really?! )(https://ch.mathworks.com/matlabcentral/answers/455329-remove-data-elements-of-the-legend-from-figure)
3) apparantly, you have no control on the legend spacing. Matlab is by default very generous with white spacings and whats better to have in a figures than white space...(https://ch.mathworks.com/matlabcentral/answers/367343-how-to-decrease-the-size-of-the-legend-in-a-figure)
Above that, Matlab makes it complicated to export figures in a controlled way. Thus, you have to use export_fig and matlab2tikz/matlab2tikz for over 8-10 years now. Matlab exportgraphics is ok-ish but it took mathwork like 8 years to come up with it and in my case, I still have to use export-fig.

Sign in to comment.

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!