2011-11-21 43 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