加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

在matlab图形的X轴刻度添加字符串(汉字)并旋转一定角度的程序

(2013-10-05 21:54:24)
标签:

matlab

xtick

xticklabel

分类: 教育

在matlab图形中,有时为了特定的需要,须在X轴刻度上添加汉字,并旋转一定的角度.特将网络的程序作了一定修改以便于运用,现将程序写在下面:

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

 

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(1)-.1*(c(2)-c(1)),length(b),1),a,'HorizontalAlignment','right','fontsize',10,'fontweight','bold','rotation',rot);
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

如果要在画出的图形X轴上添加文字,并将文字旋转一定角度,下面的例子可以借鉴:

x1=[1,2,3,4,5];
x2=[2,5,1,5,6];
h=plot(x1,x2,'LineWidth',2);
set(gca,'XTick',[1 2 3 4 5]);%
在X轴上设置刻度
set(gca,'XTicklabel',{'名称一','名称二','名称三','名称四','名称五'},'FontSize',15); %在X轴上设置的对应刻度位置标上字符串,注意要和上面一一对应
h = gca; 
th=rotateticklabel(h, 45); 
%将X轴上的文字旋转45度

所画出来的图形如下(本程序经调试,可以运行).

http://s10/mw690/5ffde3d3gx6DbZ0VHJv89&690



0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有