1

I have a Dataframe object coming from a SQL-Query that looks like this:

            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]

I want to visualize this data with a Matplotlib stackplot in python.

What works is following line:

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

What doesn't work is following line:

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

The error I get with the last line is:

"ValueError: operands could not be broadcast together with shapes (10,) (6,) "

Obviously the last line with the 10 rows x 6 columns is passed into the plotting function.. and I can't get rid of it.

Writing out each column by hand is also working but not really what I want since there will be many rows later on.

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

1 Answer 1

4

Your problem here is that df.values is a column by row array. To get the form you want you need to transpose it. Fortunately, that is easy. Replace df.values by df.values.T! So in your code replace:

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

with

plt.stackplot(df.index,df.values.T)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.