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

How to create grouped bar chart for situations

I am trying to create a grouped bar chart. In this chart, I want to compare whether they survived or died according to their embarkation points(C,Q,S). In this piece of code, i can’t decide what y is going to be.

df_drop_Emb = df_titanic.dropna(subset= ["Embarked"])
plt.figure(figsize=(10, 8))
sns.barplot(x="Embarked", 
            y="Survived",
            hue="Survived", 
            data=df_drop_Emb)
plt.ylabel("number of people", size=14)
plt.xlabel("ports", size=14)
plt.title(" the number of passengers who survived for each port", size=18)

current output but wrong

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 :

Probably need to groupby and count the lived/died in the survivors column first.

import pandas as pd
import seaborn as sns

df = pd.read_csv('train.csv')
df = df.groupby(['Embarked', 'Survived']).size().reset_index(name='count')
sns.barplot(data=df, x='Embarked',y='count',hue='Survived')

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