有没有一种方法可以分别获取轴刻度标签的尾数和指数,以便我可以操纵它们的显示方式?我需要它们看起来像“5x10-9”,而不是通常的科学计数法“5.0e-9”。
如果我在gnuplot中进行此操作,我会做类似于以下的事情
set format y "%2.0t{/Symbol \327}10^{%L}"
%2.0t可获取尾数,%L可获取指数。在matplotlib中如何实现相同的功能?
谢谢。
有没有一种方法可以分别获取轴刻度标签的尾数和指数,以便我可以操纵它们的显示方式?我需要它们看起来像“5x10-9”,而不是通常的科学计数法“5.0e-9”。
如果我在gnuplot中进行此操作,我会做类似于以下的事情
set format y "%2.0t{/Symbol \327}10^{%L}"
%2.0t可获取尾数,%L可获取指数。在matplotlib中如何实现相同的功能?
谢谢。
ScalarFormatter
采用额外的关键字参数(useMathText
),完全符合您的要求。 # import the tickers
import matplotlib.ticker as mticker
# your plotting code
# create a new formatter and use it on the y-axis
formatter = mticker.ScalarFormatter(useMathText=True)
ax.yaxis.set_major_formatter(formatter)
我在这里稍微详细地回答了一个类似的问题here
使用str.split
怎么样:
In [242]: x
Out[242]: 5e-09
In [243]: '{}*10^{}'.format(*map(int, str(x).split('e')))
Out[243]: '5*10^-9'
majorFormatter = pylab.FormatStrFormatter( '%f' )
。任何字符串操作都会被应用到 "%f"(或其他)上,而不是稍后生成的 5.0e-9。 - TheBigH