【问题标题】:Plot dataframe index绘制数据框索引
【发布时间】:2020-06-15 14:19:37
【问题描述】:

我有以下带有索引的数据框:DateTime

                  ghi        dni       dhi  ...       Ws  wind_speed  temp_air
DateTime                                    ...                               
2015-01-31   63079.57   37115.92  25963.65  ...  1589.20           0     14860
2015-02-28   68303.53   34683.19  33620.34  ...  1796.91           0     13440
2015-03-31  138789.55   88291.41  50498.14  ...  1647.59           0     14880
2015-04-30  135415.64   70234.19  65181.45  ...  1474.14           0     14400

我使用了以下方法并且它有效:

df1.plot(kind='bar',x='**gni**',y='ghi')

但如果我执行以下操作,则不会:

df1.plot(kind='bar',x='**DateTime**',y='ghi')

你能解释一下为什么吗?

【问题讨论】:

    标签: datetime plot indexing


    【解决方案1】:

    非常相似的问题here。您可以省略 x=... 或使用 .reset_index() 来获取要绘制为 x 的列:

    import pandas as pd
    
    # dummy df:
    df1 = pd.DataFrame({'ghi': [1,2,1,3,1,4],
                        'DateTime': pd.date_range('2015-01', '2015-06', freq='MS')})
    df1.set_index('DateTime', inplace=True)
    
    # don't specify x=...:
    ax = df1.plot(kind='bar', y='ghi')
    # or use the index as a column:
    ax = df1.reset_index().plot(kind='bar', x='DateTime', y='ghi')
    
    # nicer x labels, e.g.:
    ax.xaxis.set_ticklabels([i.strftime('%Y-%m-%d') for i in df1.index], rotation=45)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 2021-04-03
      • 2016-12-31
      • 1970-01-01
      • 2018-10-29
      • 2018-09-08
      相关资源
      最近更新 更多