0

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

mert
  • 153
  • 1
  • 11
  • It's still better to use `pd.crosstab` as I demonstrated in the previous question. `.groupby` requires extra steps like `.reset_index`. `pd.crosstab(df.embarked, df.survived).plot(kind='bar')` – Trenton McKinney Nov 07 '21 at 16:22

1 Answers1

1

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

Chris
  • 15,819
  • 3
  • 24
  • 37