Force exponential format of ticks LIKE MATLAB does it automatically

最后都变了- 提交于 2019-12-04 05:57:39

It doesnt appear to be possible based upon some reading, testing and property checking:

http://www.mathworks.com/matlabcentral/answers/8005-axes-tick-in-scientific-notation

The scientific notation label will only automatically appear if you have not set the YTickLabel property. If you set YTickLabel, then there is no (documented) way to get MATLAB to automatically put in the exponent the same way.

In order to get around this, if you set YTickLabel and you want the exponent, you need to text() the exponent where you want it to appear.

You can format the y axis labels like so

set(gca,'YTickLabel',sprintf('%3.1f|',get(gca,'ytick')/max(get(gca,'ytick'))

Then add the x 10^4 with TeX markup ('$\times10^4$') to get the same result manually.

I just get here because I need something similar, and I know that now there is a solution for that. Perhaps from R2015 and newer, you can set the exponent excatly with:

ax.YAxis.Exponent = 3  % 3 is for example

Or, if you want the full numbers:

ax.YAxis.Exponent = 0

While ax is the axis handle.

For me, using the '$[TeX]$' syntax doesn't work (R2012a). Instead, texlabel() works.

However, Matlab doesn't allow TeX interpreting in axis labels (at least not 2012a, and the help for 2013a doesn't seem promising: e.g. set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})

Here is a workaround, where you make your own labels by using the text() function.

plot(1:9,exp(-[1:9]))
set(gca,'XTick',[1 3 5 7 9],'XTicklabel',[])
arrayfun(@(x)text(x-.1,-.02,texlabel(sprintf('e^%d',x))),[1 5 9],'UniformOutput',false)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!