2011-11-21 45 views
1

图像的传说我有以下代码:字号为在Matlab

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,莱伊, '位置', 'SouthOutside' 可见, '字号',8);

在命令行

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

因为它的之前运行时,图像似乎通过其低部板缺如下面 (独立地存在或不属性/值“字号”的存在)观察

如果在命令行

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被抑制,一切正常。如果我希望图例的尺寸较小,我该如何解决这个问题?

回答

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

编辑:

正如我提到的,联想只是创建另一个轴。因此,对于最终控制,可以手动定位所有的轴在图中自己(指定而不是依赖于“外部”值的'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') 

screenshot

尝试一下不同的身材大小,并重新运行代码..需要注意的是,如果你希望轴自动正确地调整它们的大小,当你调整人物,你必须做类似的事情如图中'ResizeFcn'事件处理程序中的上述代码所示,即:

set(gcf,'ResizeFcn',@myEventHandler) 
+0

谢谢你的回答。我已经做了一些基于它的证明,并且我编辑了我的问题以包含它们。我认为(尽管我不确定)我的问题有两个原因:我的宽屏尺寸和属性'FontSize'的更改。尊重后者,Matlab文档帮助指出:“图例字符串的字体大小和字体名称与轴的FontSize和FontName属性相匹配”。所以,如果我通过命令图例更改它,那么轴的FontSize也会改变。 – julian

+1

@jpeji:我正在提出一个不同的解决方案,请参阅我的编辑。 – Amro

+0

谢谢你的回答。 – julian

1

它正常工作对我来说:

Result

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

我的建议是尝试直接使用'units''pixels'设置轴单位,并使用较宽的纵横比。

另一个选择可能是创建图例'orientation''horizontal',这将传播与较少的高度的项目,或将其放置在图形内,也许'SouthEast'

+0

谢谢你的回答。是的,你是对的:我的显示器有宽屏幕。正如我在@Amro的回复评论中已经提到的,经过一些证明之后,我认为FontSize属性和宽屏尺寸可能是问题的根源。 – julian