Matplotlib:y轴刻度标签使用ScalarFormatter进行格式化

时间:2016-09-25 15:11:06

标签: python matplotlib formatting

我希望y轴刻度标签的公共功率指示器为x10 ^ 8而不是1e8(默认的matplotlib行为)。以下代码很好地完成了这个:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

fig = plt.figure(figsize=(4, 4.5))

plt.bar(0, 1.25e8, 1, color='b')
plt.bar(1, 0.32e8, 1, color='r')

ax = plt.gca()
ax.set_xticklabels([])
ax.yaxis.set_major_formatter(mtick.ScalarFormatter(useMathText=True))
plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))

plt.savefig("./fig/test.png", dpi=300)
plt.show()

然而,有两个问题:

1)小数点后有明显的间隙。我怎么能摆脱它?

2)当我使用具有略微不同数据的相同脚本时,y轴标签有时仅显示一个数字。有没有办法来修复的精度 勾选标签,即使它只会在每个标签上附加零?不幸的是添加了这行

ax.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.2f'))

不起作用,因为它会覆盖ScalarFormatter行。

0 个答案:

没有答案
相关问题