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

조회 수: 32 (최근 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