How to create grouped bar chart for situations

Advertisements

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)

>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')

Leave a Reply Cancel reply