2

I need to plot data like this:

df1
id  value
ba1  4 
ba2  5
ba3  5

df2
   id     value
ba1, ba3  4
ba2       3
ba2, ba3  2
ba3       5

df3
     id       value
ba1, ba3       3
ba1, ba3, ba4  1
ba2            3
ba2, ba3       2
ba3            4
ba3, ba4       1

Every df has the same sum of value, 14. I think that would be a stacked bar plot good solution, with one bar per df, where values would be presented in percent. But I have no idea how to do from three different data frames. Everything I found there it was for the more numeric-value column from one df. Any help is welcomed. Thanks in advance.

The desired output would be something like this: example but in X would be name of dataframe

1 Answer 1

1

Something like this should work:

import pandas as pd
import matplotlib.pyplot as plt


df1 = {'id': ['ba1', 'ba2', 'ba3'], 'value': [4,5,5]}
df1 = pd.DataFrame(data=df1)
df1.set_index('id', inplace=True)

df2 = {'id': ['ba1, ba3', 'ba2', 'ba2, ba3', 'ba3'], 'value': [4,3,2,5]}
df2 = pd.DataFrame(data=df2)
df2.set_index('id', inplace=True)

df3 = {'id': ['ba1, ba3', 'ba1, ba3, ba4', 'ba2', 'ba2, ba3', 'ba3', 'ba3,\
ba4'], 'value': [3,1,3,2,4,1]}
df3 = pd.DataFrame(data=df3)
df3.set_index('id', inplace=True)

ax = plt.gca()
df1.T.plot(kind='bar', stacked=True, ax=ax, width=0.05, position=0)
df2.T.plot(kind='bar', stacked=True, ax=ax, width=0.05, position=1)
df3.T.plot(kind='bar', stacked=True, ax=ax, width=0.05, position=2)
plt.show()
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.