pyplot 删除零的数字(从 0 开始而不是 0.00)

Emm*_*t B 1 python matplotlib

我的 x 轴标签是

0.00 0.01 0.02 0.03 
Run Code Online (Sandbox Code Playgroud)

等我如何将其格式化为:

0 0.01 0.02 0.03
Run Code Online (Sandbox Code Playgroud)

我试过那些:

plt.xlim([0,0.03])

plt.xticks(np.arange(0, 0.03, 0.01))
Run Code Online (Sandbox Code Playgroud)

两者都不起作用。我认为这应该是固定的,0.00 是没有意义的。

hal*_*lex 5

您可以为 x 轴使用自定义格式化程序来打印整数:

from matplotlib.ticker import FuncFormatter

def my_formatter(x, pos):
    if x.is_integer():
        return str(int(x))
    else:
        return str(x)

formatter = FuncFormatter(my_formatter)

fig, ax = plt.subplots()
ax.xaxis.set_major_formatter(formatter)
plt.show()
Run Code Online (Sandbox Code Playgroud)