Change figure legend horizontal spacing

94 ビュー (過去 30 日間)
Matthew
Matthew 2019 年 1 月 8 日
編集済み: Karl_469 2022 年 11 月 17 日
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 件のコメント
Shubham Gupta
Shubham Gupta 2019 年 1 月 9 日
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

サインインしてコメントする。

回答 (2 件)

nbrake
nbrake 2019 年 10 月 24 日
編集済み: nbrake 2019 年 10 月 24 日
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 件のコメント
Matthew
Matthew 2019 年 10 月 26 日
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?

サインインしてコメントする。


dpb
dpb 2019 年 1 月 9 日
  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 件のコメント
Karl_469
Karl_469 2022 年 11 月 17 日
編集済み: Karl_469 2022 年 11 月 17 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by