Matplotlib轴刻度标签:获取尾数和指数

3

有没有一种方法可以分别获取轴刻度标签的尾数和指数,以便我可以操纵它们的显示方式?我需要它们看起来像“5x10-9”,而不是通常的科学计数法“5.0e-9”。

如果我在gnuplot中进行此操作,我会做类似于以下的事情

set format y "%2.0t{/Symbol \327}10^{%L}"

%2.0t可获取尾数,%L可获取指数。在matplotlib中如何实现相同的功能?

谢谢。

2个回答

2
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


1
@TheBigH,您还需要这个吗?我猜这是一个有点老的问题 ;) - hitzg
我目前并不真正需要它,但下次会派上用场。干杯! - TheBigH

0

使用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
@TheBigH 你有什么? - zhangxaochen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接