【发布时间】:2018-09-24 20:51:51
【问题描述】:
我有 pandas DataFrame、df、index 命名为 date 和列 columnA、columnB 和 columnC
我正在尝试使用 DataFrame 语法在 x 轴上散点图 index 和在 y 轴上散点图 columnA。
当我尝试时:
df.plot(kind='scatter', x='date', y='columnA')
我收到错误 KeyError: 'date' 可能是因为 date 不是列
df.plot(kind='scatter', y='columnA')
我收到一个错误:
ValueError: scatter requires and x and y column
所以在 x 轴上没有默认索引。
df.plot(kind='scatter', x=df.index, y='columnA')
我遇到错误
KeyError: "DatetimeIndex(['1818-01-01', '1818-01-02', '1818-01-03', '1818-01-04',\n
'1818-01-05', '1818-01-06', '1818-01-07', '1818-01-08',\n
'1818-01-09', '1818-01-10',\n ...\n
'2018-03-22', '2018-03-23', '2018-03-24', '2018-03-25',\n
'2018-03-26', '2018-03-27', '2018-03-28', '2018-03-29',\n
'2018-03-30', '2018-03-31'],\n
dtype='datetime64[ns]', name='date', length=73139, freq=None) not in index"
如果我直接使用matplotlib.pyplot,我可以绘制它
plt.scatter(df.index, df['columnA'])
有没有办法使用DataFrame kind 语法将索引绘制为x-axis?
【问题讨论】:
标签: python pandas matplotlib