简体   繁体   English

python matplotlib绘图日期时间索引

[英]python matplotlib plot datetime index

I am trying to create a simple line graph based on a datetime index. 我试图基于日期时间索引创建一个简单的折线图。 But I get an error message. 但是我收到一条错误消息。

#standard packages
import numpy as np
import pandas as pd

#visualization
%matplotlib inline
import matplotlib.pylab as plt

#create weekly datetime index
edf = pd.read_csv('C:\Users\j~\raw.csv', parse_dates=[6])
edf2 = edf[['DATESENT','Sales','Traffic']].copy()
edf2['DATESENT']=pd.to_datetime(edf2['DATESENT'],format='%m/%d/%Y')
edf2 = edf2.set_index(pd.DatetimeIndex(edf2['DATESENT']))
edf2.resample('w').sum()
edf2

#output

            SALES
DATESENT     
2014-01-05  476
2014-01-12  67876

Then I try to plot (the simplest line plot possible to see sales by week) 然后,我尝试绘图(最简单的折线图,可以按周查看销售额)

#linegraph
edf3.plot(x='DATESENT',y='Sales')

But I get this error message 但我收到此错误消息

KeyError: 'DATESENT' KeyError:“ DATESENT”

You're getting a KeyError because your 'DATESENT' is the index and NOT a column in edf3 . 你得到一个KeyError ,因为你的'DATESENT'是指数,而不是在一列edf3 You can do this instead: 您可以改为:

#linegraph
edf3.plot(x=edf3.index,y='Sales')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM