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

コードに見られるように、コマンドライン

legend(new_axes,Ley,'Location','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が抑制されていれば、すべて OK です。凡例のサイズを小さくしたい場合、どうすればこれを修正できますか?

4

2 に答える 2

2

それは私にとってもうまくいきます... LEGEND は基本的に Figure 内に別の軸インスタンスを作成することを理解する必要があります。

現在'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 は別の軸を作成します。したがって、究極の制御のために、Figure 内のすべての軸を自分で手動で配置できます (関数によって公開されるプロパティの「外部」値に依存するのではなく、実際の位置を指定します)。説明する例を次に示します。'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')

スクリーンショット

Figure のサイズを変更して、コードを再実行してみてください。Figure のサイズを変更するときに軸のサイズを自動的に正しく調整するには'ResizeFcn'、Figure のイベント ハンドラ内で上記のコードと同様のことを行う必要があることに注意してください。 、つまり:

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

それは私にとってはうまくいきます:

結果

スクリーンショットの縦横比が異なることに気付きました。おそらく、モニターのアスペクト比はワイドスクリーンですか?新しい軸に適用する'units' 'normalized'オプションは、表示されているモニターを基準にして寸法を設定します。幅の広い Figure を作成すると、MATLAB が凡例を下部からクリッピングしている可能性があります (そのグラフィックは完全ではありません)。

'units' 'pixels'私のアドバイスは、より正方形のアスペクト比を使用して、軸の単位を直接設定してみることです。

別のオプションは、 で凡例を作成すること'orientation' 'horizontal'です。これにより、項目がより少ない高さで広がります。または、グラフ内に配置することもできます'SouthEast'

于 2011-11-22T14:29:39.780 に答える