轴号是根据给定的^{}定义的。不幸的是(AFAIK),matplotlib没有公开一种方法来控制阈值从数字到更小的数字+偏移。一种暴力的方法是设置所有的xtick字符串:import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(100, 100.1, 100)
y = np.arange(100)
fig = plt.figure()
plt.plot(x, y)
plt.show() # original problem
# setting the xticks to have 3 decimal places
xx, locs = plt.xticks()
ll = ['%.3f' % a for a in xx]
plt.xticks(xx, ll)
plt.show()
这实际上与使用字符串设置FixedFormatter相同:from matplotlib.ticker import FixedFormatter
plt.gca().xaxis.set_major_formatter(FixedFormatter(ll))
然而,这种方法的问题是标签是固定的。如果要调整/平移绘图,必须重新开始。更灵活的方法是使用FuncFormatter:def form3(x, pos):
""" This function returns a string with 3 decimal places, given the input x"""
return '%.3f' % x
from matplotlib.ticker import FuncFormatter
formatter = FuncFormatter(form3)
gca().xaxis.set_major_formatter(FuncFormatter(formatter))
现在你可以移动绘图并保持同样的精度。但有时这并不理想。一个人并不总是想要固定的精度。人们希望保留默认的格式化程序行为,只需将阈值增加到开始添加偏移量时的值。这没有公开的机制,所以我最终要做的是更改源代码。很简单,只需在^{}中的一行中更改一个字符。如果你看一下github版本,它在第497行:if np.absolute(ave_oom - range_oom) >= 3: # four sig-figs
我通常改成:if np.absolute(ave_oom - range_oom) >= 5: # four sig-figs
发现它对我的使用很好。在matplotlib安装中更改该文件,然后记住在生效之前重新启动python。