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

pyplot remove the digits of zero ( start from 0 not 0.00)

我的 x 轴标签是

0.00 0.01 0.02 0.03 

等 我如何将其格式化为:

0 0.01 0.02 0.03

我试过那些:

plt.xlim([0,0.03])

plt.xticks(np.arange(0, 0.03, 0.01))

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

要更改特定的刻度,您可以使用自定义格式化程序,或类似以下内容:

import matplotlib.pyplot as plt

plt.plot([1,3,2])

plt.draw()      # Note, this line is important
ax = plt.gca()  # and you don't need this line if you already have an axis handle somewhere
labels = [l.get_text() for l in ax.get_xticklabels()]
labels[0] = '0'
ax.set_xticklabels(labels)

plt.show()

你用过plt.yticks([0, 0.01, 0.02, 0.03], ['0', '0.01', '0.0.2', '0.03'])吗?

您可以为您的 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()