Controlling the size of legend markers separately from the font size of legend labels

178 ビュー (過去 30 日間)
I need to control the markers in my legend separately from the font size of the legends' labels. Inspired by this previous answer, I used this code, in Matlab 2016a:
x = 1:10;
plot(x, 1*x, 'o')
hold on
plot(x, 2*x, 's')
h_legend = legend({'one','two'});
objhl = findobj(h_legend, 'type', 'line'); % objects of legend of type patch
set(objhl, 'Markersize', 99); % set marker size as desired
Funnily enough, this solution worked for me for a few plots. However, after a while, now whatever I type in place of the 99, makes no difference. If I change the 'line' into 'patch, that also makes no difference. The problem I guess comes from the fact that objhl is actually empty:
>> objhl = findobj(h_legend, 'type', 'patch')
objhl =
0x0 empty GraphicsPlaceholder array.
Any thoughts? Many thanks!

採用された回答

Subhadeep Koley
Subhadeep Koley 2020 年 2 月 4 日
A new MATLAB graphics system is introduced from MATLAB R2014b onwards. Your code will work for the older system I guess.
Use the code below if you are using MATLAB R2014b or later.
clc; close all;
x = 1:10;
plot(x, 1*x, 'o');
hold on;
plot(x, 2*x, 's');
[~, objh] = legend({'one','two'}); % Instead of "h_legend" use "[~, objh]"
objhl = findobj(objh, 'type', 'line');
set(objhl, 'Markersize', 15);
legendSize.png
  2 件のコメント
z8080
z8080 2020 年 2 月 5 日
Thank you Subhadeep!
Adam Danz
Adam Danz 2020 年 2 月 6 日
Note that since r2018b only the first output of the legend function is documented. Archived documentation for the legend function from r2016a to r2018a suggests avoiding the use of the outputs other than the first output:
Note: This syntax is not recommended and creates a legend that does not support all graphics features. Use the l = legend(__) syntax to return the legend object and set Legend Properties instead.
For example, if the first and second outputs to legend() are both included, it interferes with the ability to change the fontsize of the legend text.

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

その他の回答 (1 件)

Adam Danz
Adam Danz 2020 年 2 月 6 日
編集済み: Adam Danz 2020 年 2 月 6 日
This solution avoids the use of undocumented legend outputs that have caused problems in the past (example 1, example 2).
% Plot data
x = 1:10;
plot(x, 1*x, 'bo');
hold on;
plot(x, 2*x, 'rs');
% Create customizable objects that won't appear on the axes
% but will appear in the legend
h(1) = plot(nan, nan, 'bo', 'MarkerSize', 15, 'DisplayName', 'one');
h(2) = plot(nan, nan, 'rs', 'MarkerSize', 15, 'DisplayName', 'two');
legend(h)
  4 件のコメント
Adam Danz
Adam Danz 2022 年 3 月 30 日
> The code above changed the marker's color.
Are you refering to the code from my answer? That only changes marker size, not marker color.
Francesco Bernardini
Francesco Bernardini 2023 年 7 月 21 日
Amazing! Thanks a lot

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

カテゴリ

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

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by