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



我很难更改 Matlab R2016a 中图例中使用的字体大小。如果我使用首选语法 l = legend(),那么它可以正常工作。但是,我需要访问图标句柄才能更改facea属性。因此,我正在使用语法 [l,图标,plots,txt] = legend(),根据 Matlab,"不推荐并创建一个不支持所有图形功能的图例"。使用此语法时,字体大小无法正确更新。有没有办法获得正确的字体大小和透明的图例图标?

%% 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)

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

%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。此精细的详细行为可能会在更新的版本中略有更改。

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

最新更新