【问题标题】:Line plot in pandas series generates extra indices熊猫系列中的线图生成额外的索引
【发布时间】:2016-07-07 02:18:01
【问题描述】:

我有一个熊猫系列如下:

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 标签,例如:

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

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

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    为什么不简单地在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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-06
      • 2021-12-07
      • 2017-03-03
      • 2018-03-25
      • 2017-12-23
      • 2014-07-11
      • 1970-01-01
      相关资源
      最近更新 更多