matlab 坐标轴刻度朝外_Matlab旋转坐标轴刻度标注

MATLAB函数`rotateticklabel`用于旋转坐标轴刻度标签,可防止长字符串被裁剪。该函数接受一个轴句柄`h`和旋转角度`rot`作为参数,默认旋转90度。当刻度标签过长,如由`datetick`生成时,可能需要调整轴的位置。注意,若刻度标签包含指数,旋转后会丢失。该代码还包含一个演示示例,展示如何创建带有旋转刻度标签的图表。
摘要由CSDN通过智能技术生成

function th=rotateticklabel(h,rot,demo)

%ROTATETICKLABEL rotates tick labels

% TH=ROTATETICKLABEL(H,ROT) ris 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&gt360

% rot=rot-360;

% end

% while rot&lt0

% 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&lt180

th=text(b,repmat(c(1)-.1*(c(2)-c(1)),length(b),1),a,'HorizontalAlignment','right','fontsize',10,'fontweight','normal','rotation',rot);

%X刻度标注字体大小

else

th=text(b,repmat(c(1)-.1*(c(2)-c(1)),length(b),1),a,'HorizontalAlignment','left','fontsize',10,'fontweight','bold','rotation',rot);

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值