Pandas 和 Matplotlib:无法在数据帧上使用 Matplotlib 获得堆栈图

亚历克斯

我有一个来自 SQL-Query 的 Dataframe 对象,如下所示:

            Frage/Diskussion          ...           Wissenschaft&Technik
date                                  ...                               
2018-05-10                13          ...                              6
2018-05-11                28          ...                              1
2018-05-12                11          ...                              2
2018-05-13                21          ...                              3
2018-05-14                30          ...                              4
2018-05-15                38          ...                              5
2018-05-16                25          ...                              7
2018-05-17                23          ...                              2
2018-05-18                24          ...                              4
2018-05-19                31          ...                              4

[10 rows x 6 columns]

我想用 python 中的 Matplotlib stackplot 可视化这些数据。

有效的是以下行:

df.plot(kind='area', stacked=True)

以下行不起作用:

plt.stackplot(df.index, df.values)

我在最后一行得到的错误是:

“ValueError: 操作数无法与形状 (10,) (6,) 一起广播”

显然,最后一行 10 行 x 6 列被传递到绘图函数中.. 我无法摆脱它。

手动写出每一列也有效,但并不是我真正想要的,因为稍后会有很多行。

plt.stackplot(df.index.values, df['Frage/Diskussion'], df['Humor'], df['Nachrichten'], df['Politik'], df['Interessant'], df['Wissenschaft&Technik'])
保拉·托马斯

您的问题是df.values逐行数组。为了得到你想要的形式,你需要转置它。幸运的是,这很容易。替换df.valuesdf.values.T所以在你的代码中替换:

plt.stackplot(df.index,df.values)

plt.stackplot(df.index,df.values.T)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章