Pandas和Matplotlib:无法使stackplot在datafram上使用Matplotlib

2024-09-30 14:31:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个来自SQL查询的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来可视化这些数据。在

工作原理如下:

^{pr2}$

不起作用的是下面一行:

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

最后一行的错误是:

“值错误:操作数无法与形状(10,)(6,)”

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

手工写出每一列也很有效,但不是我想要的,因为以后会有很多行。在

plt.stackplot(df.index.values, df['Frage/Diskussion'], df['Humor'], df['Nachrichten'], df['Politik'], df['Interessant'], df['Wissenschaft&Technik'])

Tags: 对象dataframedfsqldateindex错误plt
1条回答
网友
1楼 · 发布于 2024-09-30 14:31:27

这里的问题是df.values是一个逐行数组。为了得到你想要的表格,你需要把它转置。幸运的是,这很容易。将df.values替换为df.values.T!所以在代码中替换:

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

^{pr2}$

相关问题 更多 >