使用 [l,icons,plots,txt] = legend() 时 Matlab 图例字体大小不会更新

Matlab legend font size doesn't update when using [l,icons,plots,txt] = legend()

我很难更改 Matlab R2016a 中绘图图例中使用的字体大小。如果我使用首选语法 l = legend() 那么它就可以正常工作。但是,我需要访问图标句柄才能更改 facea 属性。因此,我使用语法 [l,icons,plots,txt] = legend() ,根据 Matlab "is not recommended and creates a legend that does not support all graphics features." 使用此语法时,字体大小无法正确更新。有没有办法得到正确的字体大小和透明图例图标?

%% Some data to plot
x=linspace(1,10);
y=linspace(1,20);
[xx,yy]=meshgrid(x,y);
zz1=2*xx+3*xx.*yy+yy.^2;

%% Correct font, but icons not transparent
figure(1)
h=surf(x,y,zz1,'FaceColor','b','EdgeColor','none');
alpha(h,0.4)
l=legend('plot1');
l.FontSize=24;
l.FontName='Wide Latin';

%% Icons transparent, but incorrect font
figure(2)
h=surf(x,y,zz1,'FaceColor','b','EdgeColor','none');
alpha(h,0.4)
[l,icons,plot,text]=legend('plot1');
l.FontSize=24;
l.FontName='Wide Latin';
set(findobj(icons,'type','patch'),'facea',0.4)

在多输出调用中,字体大小似乎附加到 Legend 的第二个输出。试试这个例子:

%Plot some data and a legend
[X,Y,Z] = peaks;
surf(X,Y,Z)
[LEGH,OBJH,OUTH,OUTM] = legend('Peaks legend');

%Change the transparency of the legend patches
OBJH(2).FaceAlpha = 0.4;  %Your "findobj" call is probably more reliable.

%Change the fontsize
OBJH(1).FontSize = 6;   %Your "findobj" call is probably more reliable.

%If you change the "fontsize" of the main legend object, 
%    that seems to change the size of the legend box, but not the contents.
LEGH.FontSize = 16;
%This could actually be useful, for example to fine tune the size 
%    of the box. But it is pretty counterintuitive.

直接适用于您的代码:

%% Altogether now
figure(3)
h=surf(x,y,zz1,'FaceColor','b','EdgeColor','none');
alpha(h,0.4)
[l,icons,plot,text]=legend('plot1');
%Set the objects that you want
set(findobj(icons,'type','Text'),'FontSize',24)
set(findobj(icons,'type','Text'),'FontName','Wide Latin')
set(findobj(icons,'type','patch'),'facea',0.4)

%Make painful adjustments to item positioning (there should be a better way ...)
l.FontName='Wide Latin';  %These two lines get the box about the right size
l.FontSize=24;
icons(2).Vertices([3 4],1) = 0.2; %This squeezes the color patch to the left
icons(1).Position(1) = 0.3;       %This moves the text to the left to fit in the box

(我目前是 运行 R2015a。这种精细的行为可能会在您更新的版本中略有变化。)

有同样的问题 - 我可以 运行 icons(ii).FontSize 通过单步执行或从命令行进入循环,但不会在脚本中响应。
我在循环之前添加了一个暂停(0.1),瞧!