如何在不更改DataFrame标题中的值的情况下更改matplotlib中的x轴

时间:2018-09-05 08:20:02

标签: python pandas matplotlib

我有一个DataFrame:

 index    0     1     2     3     4     5
 1        12    13    14    15    13    12


 df.plot()

x轴的范围是0-5,但是我想更改为1-6,并且不想通过df.columns = df.columns + 1更改列的标题值。

1 个答案:

答案 0 :(得分:1)

您可以从df.plot()返回轴对象,然后使用函数set_xticklabels()修改x轴刻度标签:

ax = df.plot()
ax.set_xticklabels([1,2,3,4,5,6])

请注意,这使用了pandas / matplotlib自动生成的刻度位置。