Change figure legend horizontal spacing
124 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
![example legend.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/200407/example%20legend.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:
- Space between a marker and a marker entry
- Space between the first legend entry and the second marker
Thanks!
1 commentaire
Shubham Gupta
le 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
Réponses (2)
nbrake
le 24 Oct 2019
Modifié(e) : nbrake
le 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;
dpb
le 9 Jan 2019
- Something legend gives no control of directly
- 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 commentaire
Karl_469
le 17 Nov 2022
Modifié(e) : Karl_469
le 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.
Voir également
Catégories
En savoir plus sur Legend dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!