有没有一种简单的方法可以让 matplotlib 在对数图中不显示 10 的幂,而只显示数字?即,不是显示
[10^1, 10^2, 10^3]
而是 [10, 100, 1000]
?我不想更改刻度线位置,只是想摆脱十的幂。
这是我目前拥有的:
我可以通过
xticks
更改标签本身,但是随后我会得到 y 刻度标签的字体或大小不匹配的信息。我在本文中使用 TeX。我尝试过以下方法:
xx, locs = xticks()
ll = [r'\rm{%s}' % str(a) for a in xx]
xticks(xx, ll)
这给出了以下结果:
在这种特殊情况下,我可以使用相同的 LaTeX 罗马字体,但大小和外观与 y 轴上的字体不同。另外,如果我在 matplotlib 中使用不同的 LaTeX 字体,这将会出现问题。
有没有更灵活的方法来关闭十记法的力量?
ScalarFormatter
:
from matplotlib import rc
rc('text', usetex=True)
rc('font', size=20)
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111)
ax.semilogx(range(100))
ax.xaxis.set_major_formatter(ScalarFormatter())
plt.show()
对于较新的 matplotlib 版本,请使用 plt.FuncFormatter。
例如:
def format_func(value, tick_number):
return '%g' % (value)
ax.xaxis.set_major_formatter(plt.FuncFormatter(format_func))