Change legend line style

2019-02-20 16:36发布

I am playing with the visual effect of plots, and a question came up while changing the style of a legend.

To be able to save the figure with legends big enough that can be seen usually one needs to change the FontSize property to e.g. 24. When you do that, the size of the font changes, however, the small line next to it has the same size than when it was small. The proportion between line/text seem quite appropriate to me with a FontSize of around 10, while I believe that with big font sizes bigger "eat" visually the line, which is the important part.

Example with fontsize 30 and 10 (please ignore how much I suck in mspaint and the low resolution of the zoomed legend). The proportion between line/text is nicer in the small one.

enter image description here

I was wondering if there is a way to modify that line. I have been checking the properties but I haven't found any relevant one.

NOTE: The LineWidth property does not change the width of the colour lines, but the width of the bounding box.

1条回答
Evening l夕情丶
2楼-- · 2019-02-20 17:04

You could play with the outputs arguments of legend, especially the icons variable (check here).

According to the docs, they correspond to

Objects used to create the legend icons and descriptions, returned as text, patch, and line object.

Therefore you might use something like this to modify the LineWidth property of any of your plot, or both of course:

clear
clc
close all
x = 1:10;

plot(x,rand(1,10));
hold on;

plot(x,x,'k');

[h,icons,plots,str] = legend('First plot','Second plot','Location','NorthWest');

set(h,'FontSize',30);

set(icons(:),'LineWidth',2); %// Or whatever

Which outputs:

enter image description here

Note that I used R2014a so it might be a bit different for R2014b.

查看更多
登录 后发表回答