Clear Filters
Clear Filters

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

18 views (last 30 days)
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.

Accepted Answer

Voss
Voss on 19 Aug 2023
Edited: Voss on 19 Aug 2023
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 Comments
David Gillcrist
David Gillcrist on 19 Aug 2023
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 on 19 Aug 2023
lgd.ItemTokenSize(1) seems to be the width of the icons (lines) in the legend, and lgd.ItemTokenSize(2) seems to be the height.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!