Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Grouped bar plot on the x-axis

I have a problem. I would like to print a grouped bar plot (with seaborn or matplotlib). When I run the below snippet code I got

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
d = {'type': ['House', 'Boat', 'Bus', 'Bus', 'Boat'], 'room': ['private', 'public', 'public', 'private', 'nothing']}
df = pd.DataFrame(data=d)
print(df)

[OUT]

    type     room
0  House  private
1   Boat   public
2    Bus   public
3    Bus  private
4   Boat  nothing

g = sns.catplot(
    data=df, kind="bar",
    x='type', y=df.groupby(['type']).count(), hue="room",
    palette="dark", alpha=.6, height=6
)
g.despine(left=True)
g.set_axis_labels("", "Counts")
g.legend.set_title("")

[OUT]
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What I want

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Looks like this would do the trick:

pd.crosstab(df['type'],df['room']).plot.bar(stacked=True)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading