[转载]MATLAB 自定义坐标刻度中使用上角标(上标)和下角标(下标)
2014-02-26 20:16阅读:
在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','S
tring',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