How to set legend marker size

218 ビュー (過去 30 日間)
RuiQi
RuiQi 2017 年 7 月 16 日
編集済み: Monique 2024 年 7 月 24 日
How do I change the marker size on the legend ? I can change the font size but not marker.
l = legend('Orientation', 'Horizontal', 'RNN (Ours)', 'SLIC', 'SEEDS', 'LSC', 'ERS', 'FH');
l.FontSize = 20;
%l.MarkerSize = 20; does not work
%l.markersize = 20; does not work
set(l,'Position', [0.4 0 0.2 0.2], 'Units', 'normalized');
%set(l,'MarkerSize', 20); does not work

回答 (3 件)

Carl Witthoft
Carl Witthoft 2019 年 4 月 11 日
Best way: see Stackoverflow question 2871747
% thanks to , Luis Mendo and Lea
[~, objh] = legend({'one plot', 'another plot'}, 'location', 'NorthWest', 'Fontsize', 14);
%// set font size as desired
% note that even if you plot(x,y,'.') it's a "line" plot
objhl = findobj(objh, 'type', 'line'); %// objects of legend of type line
set(objhl, 'Markersize', 12); %// set marker size as desired
% or for Patch plots
objhl = findobj(objh, 'type', 'patch'); % objects of legend of type patch
set(objhl, 'Markersize', 12); % set marker size as desired
  11 件のコメント
Gayan Lankeshwara
Gayan Lankeshwara 2020 年 6 月 21 日
I am working in Matlab 2019a, but Carl's code does not work in my case for a line plot.
objhl = findobj(objh, 'type', 'line'); %// objects of legend of type line
set(objhl, 'Markersize', 12); %// set marker size as desired
I mean, even if I change the value of MarkerSize, nothing changes.
Any update ?
Thanks.
Walter Roberson
Walter Roberson 2020 年 6 月 21 日
Are you using legend() with at least two output arguments? If you only have one output for legend then legend works differently.

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


Akira Agata
Akira Agata 2017 年 7 月 16 日
How about changing the marker size of your plot? Here is an example.
plot(magic(4),'o','MarkerSize',10);
l = legend('a','b','c','d');
  2 件のコメント
Arnoud Jochemsen
Arnoud Jochemsen 2022 年 2 月 15 日
Note that this only works up to a certain size. When using 15 instead of 10, the legend markers are clearly smaller than the plot markers.
plot(magic(4),'o','MarkerSize',15);
l = legend('a','b','c','d');
Monique
Monique 2024 年 7 月 24 日
編集済み: Monique 2024 年 7 月 24 日
I just discovered something interesting (likely a bug). If you ask for 2 outputs, then the legend markers are actually the correct size. No idea why. (This is R2024a).
plot(magic(4),'o','MarkerSize',15);
[l1,l2] = legend('a','b','c','d');

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


Walter Roberson
Walter Roberson 2017 年 7 月 16 日
  2 件のコメント
Fernon Ejikhoudt
Fernon Ejikhoudt 2018 年 7 月 11 日
I have the same problem and I'm using 2018a. Is there an answer to this. otherwise it almost makes the printed graphs useless because of the extremely tiny dots.
Walter Roberson
Walter Roberson 2018 年 7 月 11 日
Did you experiment with Kelly's legendflex() ?

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by