Is there a MATLAB official way to increase/decrease line length/icon size in legends?

30 次查看(过去 30 天)
I'm making a a plot where the legend is quite long, here is minimal working example
plot(rand(4))
lgd = legend(["Directional Data","Median Direction",...
"Interquartile Range","Interquartile Slope Data"],...
"NumColumns",4,"Interpreter","latex");
lgd.Position = [0.4,0.0,0.2,0.05];
I would like to know if there is an official way to shorten the lengths of the lines for "Median Direction" and "Interquartile Range"? I have found several questions asking how to do this or similar things (you can look at the answers here: How can I modify the lengh of the lines in a LEGEND? - MATLAB Answers - MATLAB Central (mathworks.com)). Does MATLAB not have an official way of doing this? So far I've tried every answer I've come across and nothing seems to change the length of my legend lines.

采纳的回答

Voss
Voss 2023-8-19
编辑:Voss 2023-8-19
I don't know if there's an official way, given that the outputs from legend() beyond the first one are not mentioned in the official documentation. And you're right, when you specify NumColumns and request the 2nd (or subsequent) output, NumColumns seems to be ignored, so the accepted answer to the linked-to question doesn't work in this case.
Here's a workaround (using the approach taken by the other answer to the linked-to question) that may be good enough, which decreases the length of all the lines in the legend.
plot(rand(4))
lgd = legend(["Directional Data","Median Direction",...
"Interquartile Range","Interquartile Slope Data"],...
"NumColumns",4,"Interpreter","latex");
lgd.Position = [0.4,0.0,0.2,0.05];
lgd.ItemTokenSize(1) = 12;
  4 个评论
David Gillcrist
David Gillcrist 2023-8-19
What's the difference between lgd.ItemTokenSize(1) and lgd.ItemTokenSize(2). I never found that being explcitly explained, so I thought it'd be a good question?
Voss
Voss 2023-8-19
lgd.ItemTokenSize(1) seems to be the width of the icons (lines) in the legend, and lgd.ItemTokenSize(2) seems to be the height.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by