熊猫系列中的线图生成额外的指数

时间:2016-07-06 15:40:36

标签: python pandas matplotlib

我有一个Pandas系列如下:

2014    5668
2015    6024
2016    3903
Name: year, dtype: int64

我尝试绘制线图,其中x轴标签为年,y轴标签为对应值。我这样做:

ax = year_counts.plot(kind='line', figsize=[10, 5], marker='o')
ax.yaxis.grid(True)
ax.set_xticklabels(year_counts.index)rotation_mode='anchor', ha='center')
plt.show()

但是当我查看图表时,我会看到两个额外的x标签,例如:

Line plot

我不明白为什么会这样。如果我用set_xticklabels删除该行,我没有相同的问题,但我有0,1,2作为标签而不是2014年,2015年和2016年。

看起来我需要至少五个索引值对来生成一个图(据我所知)。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

为什么不指定要在year_counts.plot()中绘制的列?例如:

import pandas as pd

data = [[2014, 5668],
        [2015, 6024],
        [2016, 3903]]

df = pd.DataFrame(data, columns=['years','value'])

df.plot('years', 'value')