Matlab 自定义figure上下标

转载自:http://blog.sina.com.cn/s/blog_45b486b801018qoj.html

在matlab自带的格式中无法在自定义坐标刻度中使用上角标(上标)和下角标(下标),在标题中可用的_作为下角标和^作为上角标都不行了。
在matlab帮助中写着:
from matlab help
Note that tick labels do not interpret TeX character sequences (however, the Title, XLabel, YLabel, and ZLabel properties do).

通过搜索,发现网上牛人自己编写了如下函数用来实现此功能:
(来自于http://www.ilovematlab.cn/thread-39718-1-1.html)learnm写的:

function settick(axis,ticks)

n=length(ticks);
tkx=get(gca,'XTick');tky=get(gca,'YTick');
switch axis
      case 'x'
              w=linspace(tkx(1),tkx(end),n);
              set(gca, 'XTick', w, 'XTickLabel', []);%刷新刻度,去掉刻度值
              yh=(14*w(1)-w(end))/13;%按坐标轴比例调整刻度纵坐标位置
              for i=1:n
                    text('Interpreter','tex','String',ticks(i),'Position',[w(i),yh],'horizontalAlignment', 'center');
              end
      case 'y'
              w=linspace(tky(1),tky(end),n);
              set(gca, 'YTick', w, 'YTickLabel', []);
              xh=(11*w(1)-w(end))/10;
              for i=1:n
                    text('Interpreter','tex','String',ticks(i),'Position',[xh,w(i)],'horizontalAlignment', 'center');
              end
end
--------------------------
调用方法为:
例如:
>> x=0:0.1:4*pi;plot(x,sin(x));ticks={'G_1'    'G_2'    'G_3'    'G_4'    'G_5'};settick('x',ticks)
>> figure;x=0:0.1:4*pi;plot(x,sin(x));ticks={'G_1'    'G_2'    'G_3'    'G_4'    'G_5'};settick('y',ticks)

但是经过试用发现,X坐标轴的刻度的位置不对,于是自己进行了小小的修改,变成如下所示,就很完美了。

function settickfsun(axis,ticks)
n=length(ticks);
tkx=get(gca,'XTick');tky=get(gca,'YTick');
switch axis
       case 'x'
               w=linspace(tkx(1),tkx(end),n);
               set(gca, 'XTick', w, 'XTickLabel', []);%刷新刻度,去掉刻度
                yh=-0.06*max(tky);%按坐标轴比例调整刻度横坐标的y位置
               for i=1:n
                       text('Interpreter','tex','String',ticks(i),'Position',[w(i),yh],'horizontalAlignment','center','VerticalAlignment','bottom');
               end
       case 'y'
               w=linspace(tky(1),tky(end),n);
               set(gca, 'YTick', w, 'YTickLabel', []);
        xh=-0.06*max(tkx);%按坐标轴比例调整刻度纵坐标的x位置
               for i=1:n
                       text('Interpreter','tex','String',ticks(i),'Position',[xh,w(i)],'horizontalAlignment', 'center');
               end
end


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值