我必须绘制此图 click here to view it
这是fdata.csv文件:
Date,Open,High,Low,Close
03-10-16,774.25,776.065002,769.5,772.559998
04-10-16,776.030029,778.710022,772.890015,776.429993
05-10-16,779.309998,782.070007,775.650024,776.469971
06-10-16,779,780.47998,775.539978,776.859985
07-10-16,779.659973,779.659973,770.75,775.080017
我已经这样编码:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('fdata.csv', index_col=0)
df.plot(x=df.index, y=df.columns)
plt.show()
但是显示错误:
KeyError: "Index(['03-10-16', '04-10-16', '05-10-16',
'06-10-16', '07-10-16'], dtype='object', name='Date')
not in index"
任何人都可以解释并解决我的问题吗?
答案 0 :(得分:0)
通过添加参数DatetimeIndex
创建parse_dates
,然后在所有列中调用DataFrame.plot
:
df = pd.read_csv('fdata.csv', index_col=0, parse_dates=[0])
df.plot()