Python 3.x KeyError:“;DatetimeIndex([';2006-12-31';,';2007-12-31';],\n数据类型=';日期时间64[ns]';,名称=';日期';,频率=无)不在索引中

Python 3.x KeyError:“;DatetimeIndex([';2006-12-31';,';2007-12-31';],\n数据类型=';日期时间64[ns]';,名称=';日期';,频率=无)不在索引中,python-3.x,matplotlib,datavisualization.toolkit,Python 3.x,Matplotlib,Datavisualization.toolkit,我第一次使用matplotlib在条形图、柱形图、直线图和散点图中可视化我的数据。每当我提到x轴和y轴时,我都会得到一个错误。下面是我所有图表的代码 df.plot.bar(pd.DatetimeIndex(df['date']), y=df[value]) df.plot.bar(pd.DatetimeIndex(df['date']), y=df[value]) df.plot.line(pd.DatetimeIndex(df['date']), y=df[value]) df.plot.s

我第一次使用matplotlib在条形图、柱形图、直线图和散点图中可视化我的数据。每当我提到x轴和y轴时,我都会得到一个错误。下面是我所有图表的代码

df.plot.bar(pd.DatetimeIndex(df['date']), y=df[value])
df.plot.bar(pd.DatetimeIndex(df['date']), y=df[value])
df.plot.line(pd.DatetimeIndex(df['date']), y=df[value])
df.plot.scatter(x=pd.DatetimeIndex(df['date']), y=df[value])
plt.show()

我也发现了类似的问题。但这并不能解决我的问题。

我发现了我的错误。我需要通过列名定义x轴和y轴,而不需要提及像df['date']。但是,我使用散点矩阵将散点图可视化。所以可行的代码是

df.plot.bar(pd.DatetimeIndex(x='date', y='value')
df.plot.bar(pd.DatetimeIndex(x='date', y='value')
df.plot.line(pd.DatetimeIndex(x='date', y='value')
scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')
plt.show()

我发现了我的错误。我需要通过列名定义x轴和y轴,而不需要提及像df['date']。但是,我使用散点矩阵将散点图可视化。所以可行的代码是

df.plot.bar(pd.DatetimeIndex(x='date', y='value')
df.plot.bar(pd.DatetimeIndex(x='date', y='value')
df.plot.line(pd.DatetimeIndex(x='date', y='value')
scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde')
plt.show()

您并不是要为
x
y
参数提供完整的数据,而是要使用的列的名称。否则您可能想直接使用matplotlib,
plt.plot()
plt.bar()
谢谢,我知道。但我需要提到它,以便将来有更多的用途。我的意思是,我的x轴将始终引用日期列,但y轴将被更改。您不需要为
x
y
参数提供完整的数据,而是要使用的列的名称。否则您可能想直接使用matplotlib,
plt.plot()
plt.bar()
谢谢,我知道。但我需要提到它,以便将来有更多的用途。我的意思是,我的x轴总是指日期列,但y轴会改变。