使用Series.plot()绘制每小时数据

时间:2019-03-31 17:21:46

标签: python datetime matplotlib series

我有一个csv文件,如下所示:

df = pd.read_csv('book1.csv',  header=1)

    Index   Date Time      DEMAND
    0   2005-03-01 06:30:00 1
    1   2005-03-01 06:45:00 2
    2   2005-03-01 07:00:00 4
    3   2005-03-01 07:15:00 0
    4   2005-03-01 07:30:00 10
    5   2005-03-01 07:45:00 13

我想绘制它,但是当我调用df.plot()时,出现错误:

df.plot()

ValueError: view limit minimum -36601.34583333333 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units

我还尝试过通过以下方式创建时间序列:

pd.Series(data=df['DEMAND'], index=df['Date Time'])

但是结果是数据变为空:

Date Time
2005-03-01 06:30:00   NaN
2005-03-01 06:45:00   NaN
2005-03-01 07:00:00   NaN
2005-03-01 07:15:00   NaN
2005-03-01 07:30:00   NaN
2005-03-01 07:45:00   NaN
2005-03-01 08:00:00   NaN
2005-03-01 08:15:00   NaN
2005-03-01 08:30:00   NaN

我正在寻找

1)关于这些方法为何失败的建议,和/或

2)另一种获得我想要的结果的方法。

谢谢!

1 个答案:

答案 0 :(得分:0)

我确实提出了一种解决方案:

x = df['Date Time']
y = df['DEMAND']

plt.plot(x,y)

enter image description here

仍然想弄清楚为什么我的方法有问题。