科学的刻度标签表示法调整 [英] Scientific tick label notation tuning
问题描述
我对刻度标签的默认科学格式并不完全满意.例如,
I am not completely satisfied with the default scientific formatting for the tick labels. For example,
import numpy as np
import pylab
pylab.rcParams['text.usetex'] = True
pylab.ticklabel_format(style='sci')
pylab.figure(1)
pylab.clf()
pylab.scatter(range(10),np.linspace(-21.305,-21.300,10))
pylab.show()
提供一张带有刻度标签
这里的问题是首先我认为-2.1298x10^1
应该是-21.298
,通常我只希望看到仅被3(10^3
,10^6
等除的指数,对于10^0
我想像-21.298
一样将其隐藏.)
What's wrong in here is firstly that -2.1298x10^1
should be in my opinion -21.298
, and in general I would only like to see only exponents divisible with 3 (10^3
, 10^6
etc and in the case of 10^0
i would like to hide it as in -21.298
).
此外,我想用0
和-2
代替0.000
和-0.002
,以便将指数10^-3
包括在-21.298
中,以便总的滴答标签会
Also, I would like to have instead of 0.000
and -0.002
and so on 0
and -2
so that the exponent 10^-3
is included to the -21.298
so that in total the ticklabels would be
x 10^-3 - 21.298
0
-2
-4
-6
现在,我是否错过了一些简单的方法来做到这一点?预先谢谢你.
Now, have I missed some easy way to do this? Thank you in advance.
推荐答案
You can achieve it using the ScalarFormatter
:
from matplotlib.ticker import ScalarFormatter
sf = ScalarFormatter()
sf.set_scientific(True)
sf.set_powerlimits((-0.00001, 0.00001))
ax = pylab.gca()
ax.yaxis.set_major_formatter(sf)
pylab.show()
您可以通过将数字传递给format_data()
来测试格式化程序将如何处理给定数字:
You can test what the formatter will do to a given number by passing the number to format_data()
:
sf.format_data(0.002)
#'2{\\times}10^{-3}'
这篇关于科学的刻度标签表示法调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!