1

Rotate Tick Labelを使用して、x 軸のラベルを垂直方向に変更しています。

目盛りラベルの回転機能を使用する前は、x 軸のラベルはグラフの下部にあります。

ここに画像の説明を入力

次のコマンドの後:

TH= rotateTickLabel(gca,90);

グラフは次のようになります。

ここに画像の説明を入力

ラベルをグラフの下部にとどめ、上部に移動しないようにするにはどうすればよいですか?

4

1 に答える 1

2

このエラーが発生する理由は、'plot' で作成されたプロットでは、YTicks が下から上に定義されるためです。また、イメージ プロット ('image' で作成) では、YTicks は上から下に定義されます。次の 2 つの変更を、rotateticklabel.m に加えます。

54 行目を次のように編集します。

th=text(b,repmat(c(end)-.1*(c(end-1)-c(end)),length(b),1),a,'HorizontalAlignment','right','rotation',rot);

56 行目を次のように編集します。

th=text(b,repmat(c(end)-.1*(c(end-1)-c(end)),length(b),1),a,'HorizontalAlignment','left','rotation',rot);

完了するには、rotateticklabel.m 全体が次のようになります。

function th=rotateticklabel(h,rot,demo)
%ROTATETICKLABEL rotates tick labels
%   TH=ROTATETICKLABEL(H,ROT) is the calling form where H is a handle to
%   the axis that contains the XTickLabels that are to be rotated. ROT is
%   an optional parameter that specifies the angle of rotation. The default
%   angle is 90. TH is a handle to the text objects created. For long
%   strings such as those produced by datetick, you may have to adjust the
%   position of the axes so the labels don't get cut off.
%
%   Of course, GCA can be substituted for H if desired.
%
%   TH=ROTATETICKLABEL([],[],'demo') shows a demo figure.
%
%   Known deficiencies: if tick labels are raised to a power, the power
%   will be lost after rotation.
%
%   See also datetick.

%   Written Oct 14, 2005 by Andy Bliss
%   Copyright 2005 by Andy Bliss

%DEMO:
if nargin==3
    x=[now-.7 now-.3 now];
    y=[20 35 15];
    figure
    plot(x,y,'.-')
    datetick('x',0,'keepticks')
    h=gca;
    set(h,'position',[0.13 0.35 0.775 0.55])
    rot=90;
end

%set the default rotation if user doesn't specify
if nargin==1
    rot=90;
end
%make sure the rotation is in the range 0:360 (brute force method)
while rot>360
    rot=rot-360;
end
while rot<0
    rot=rot+360;
end
%get current tick labels
a=get(h,'XTickLabel');
%erase current tick labels from figure
set(h,'XTickLabel',[]);
%get tick label positions
b=get(h,'XTick');
c=get(h,'YTick');
%make new tick labels
if rot<180
    th=text(b,repmat(c(end)-.1*(c(end-1)-c(end)),length(b),1),a,'HorizontalAlignment','right','rotation',rot);
else
    th=text(b,repmat(c(end)-.1*(c(end-1)-c(end)),length(b),1),a,'HorizontalAlignment','left','rotation',rot);
end

次の短い例のように、rotateticklabel.m を編集すると、'image' で作成されたプロットで正常に使用できるようになります。

A = magic(5);
image(A)
datetick('x',0,'keepticks')
h=gca;
set(h,'position',[0.13 0.35 0.775 0.55])
rot=90;
th = rotateticklabel(h,30)
于 2012-05-08T05:09:51.090 に答える