1

我有以下代码:

X = 0:pi/100:0.25*pi;
Y1 = sin(X);
Y2 = cos(X);
Y3 = tan(X);
fh = figure('toolbar','none','menubar','none','Units','characters');
Pan1 = uipanel(fh,'Units','normalized','Position',[0 0 0.5 1],'title',...
  'Panel1');
Pan2 = uipanel(fh,'Units','normalized','Position',[0.5 0 0.5 1],'title',...
  'Panel2');
haxes = axes('Parent',Pan2,'Units', 'normalized','Position',...
[0.125 0.1 0.75 0.75]);
hplot = plot(haxes,X,Y1,X,Y2,X,Y3);
xlabel(haxes,'Time (second)');
ylabel(haxes,'Amplitude (meter)');
title(haxes,'Trigonometric functions');
Ley = {'Sine function','Cosine function','Tangent function'}; %# legend's strings values
legend(haxes,Ley,'Location','SouthOutside');
[FileName,PathName,FilterIndex] = uiputfile('*.bmp;*.png;*.jpg;*.tif','Save as');
ftmp = figure('Menu','none','Toolbar','none','Units','normalized',...
  'Position',[-1000 -1000 1 1]);
set(gcf,'PaperPositionMode','auto');
set(gcf,'InvertHardcopy','off');
new_axes = copyobj(haxes, ftmp);
legend(new_axes,Ley,'Location','SouthOutside','FontSize',8);
set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);
fmtgraf = {'-dbmp','-dpng','-djpeg','-dtiff'};
fmt = fmtgraf{FilterIndex};
print(ftmp,fmt,FileName,'-r0');
delete(ftmp);
delete(fh);

从代码中可以看出,命令行

图例(new_axes,Ley,'位置','SouthOutside','FontSize',8);

在命令行之前运行

 set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);

正因为如此,图像看起来被它的低部分切割,如下所示(与属性/值“FontSize”的存在或不存在无关)

如果命令行

 legend(new_axes,Ley,'Location','SouthOutside','FontSize',8);

在命令行之后运行

 set(new_axes,'Units','normalized','Position',[0.1 0.1 0.8 0.8]);

现在图像被其低部分切割,但在这种情况下,既看不到 xlabel 文本也看不到图例框(如下所示)

如果'FontSize',8被压制,一切正常。如果我希望图例的尺寸更小,我该如何解决这个问题?

4

2 回答 2

2

它对我来说也很好用......你必须明白,LEGEND 基本上在图中创建了另一个轴实例。

现在您正在使用'SouthOutside'位置放置它,因此它会尝试调整现有轴的大小以将其自身放置在其下方,但如果您不留出足够的空间,它可能不适合,特别是当您使用'normalized'让轴自动-在给定父容器大小的情况下调整大小。

尝试提前一点垂直收缩主情节轴,给传说更多的空间......

命令的顺序也很重要。比较一下:

new_axes = copyobj(haxes, ftmp);
legend(new_axes, Ley, 'Location','SouthOutside', 'FontSize',8);
set(new_axes, 'Units','normalized', 'Position',[0.1 0.1 0.8 0.8]);

反对:

new_axes = copyobj(haxes, ftmp);
set(new_axes, 'Units','normalized', 'Position',[0.1 0.1 0.8 0.8]);
legend(new_axes, Ley, 'Location','SouthOutside', 'FontSize',8);

编辑:

正如我所提到的,LEGEND 只是创建了另一个轴。因此,为了最终控制,您可以自己手动定位图中的所有轴(指定实际位置,而不是依赖函数公开的属性的“外部”值)。下面是一个示例来说明:'Location'legend

%# create a normal plot
clf
hAx = axes();
plot(hAx, rand(10,3))
xlabel(hAx, 'xlabel'), title(hAx,'title')

%# add a legend on the inside and record the axis outerposition (in pixels)
hLgnd = legend(hAx, {'1' '2' '3'}, 'Location','South', 'FontSize',8);
set(hLgnd, 'Units','pixels')
op = get(hLgnd,'OuterPosition');
set(hLgnd, 'Units','normalized')

%# resize the plot axis vertically to make room for the legend
set(hAx, 'Units','pixels')
pos = get(hAx,'Position');
ins = get(hAx,'TightInset');
set(hAx, 'Position',[pos(1) pos(2)+op(4) pos(3) pos(4)-op(4)])
set(hAx, 'Units','normalized')

%# move the legend to the bottom in the free space
set(hLgnd, 'Units','pixels')
set(hLgnd, 'OuterPosition',[op(1) (pos(2)-ins(2))/2 op(3) op(4)])
set(hLgnd, 'Units','normalized')

截屏

尝试不同的图形大小并重新运行代码。请注意,如果您希望轴在调整图形大小时自动正确调整其大小,则必须在图形的'ResizeFcn'事件处理程序中执行与上述代码类似的操作, IE:

set(gcf,'ResizeFcn',@myEventHandler)
于 2011-11-22T17:31:09.933 回答
1

这对我来说可以:

结果

我注意到我们的屏幕截图有不同的纵横比。也许您的显示器具有宽屏宽高比?'units' 'normalized'您应用于新轴的选项将设置其尺寸相对于它所显示的监视器。当您创建一个更宽的图形时,也许 MATLAB 正在从底部剪切图例(它的图形并不完美)。

我的建议可能是尝试直接使用 设置轴单位'units' 'pixels',具有更平方的纵横比。

另一种选择可能是使用 来创建图例'orientation' 'horizontal',这会将项目以较小的高度展开,或者将其放置在图形内,也许是'SouthEast'

于 2011-11-22T14:29:39.780 回答