我的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是没有意义的。
答案 0 :(得分:4)
您可以为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()
答案 1 :(得分:1)
要更改特定标记,您可以使用自定义格式化程序,或类似以下内容:
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()
答案 2 :(得分:1)
您使用过plt.yticks([0, 0.01, 0.02, 0.03], ['0', '0.01', '0.0.2', '0.03'])
吗?