In titanic dataset, I need to create a chart that shows the percentage of passengers for all class who survived. Also it should have three pie charts. class 1 survived and not-survived, class 2 survived and not-survived, class 3.
How can make this happen? I already tried this type of code but it produces wrong values.
import pandas as pd
import seaborn as sns # for dataset
df_titanic = sns.load_dataset('titanic')
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
c1s = len(df_titanic[(df_titanic.pclass==1) & (df_titanic.survived==1)].value_counts())
c2ns = len(df_titanic[(df_titanic.pclass==1) & (df_titanic.survived==0)].value_counts())
this code produce true values but I need that in 3 pie chart
df_titanic.groupby(['pclass' ,'survived']).size().plot(kind='pie', autopct='%.2f')
class: 1,2,3 survived: 0,1
pandas.crosstab
is used to shape the dataframe
pandas.DataFrame.pivot
and pandas.DataFrame.pivot_table
are other options for reshaping data for plotting.pandas.DataFrame.plot
with kind='pie'
and subplots=True
.python 3.8.12
, pandas 1.3.4
, matplotlib 3.4.3
import seaborn as sns # for titanic data only
import pandas as pd
from matplotlib.patches import Patch # to create the colored squares for the legend
# load the dataframe
df = sns.load_dataset('titanic')
# reshaping the dataframe is the most important step
ct = pd.crosstab(df.survived, df.pclass)
# display(ct)
pclass 1 2 3
survived
0 80 97 372
1 136 87 119
# plot and add labels
colors = ['tab:blue', 'tab:orange'] # specify the colors so they can be used in the legend
labels = ["not survived", "survived"] # used for the legend
axes = ct.plot(kind='pie', autopct='%.1f%%', subplots=True, figsize=(12, 5),
legend=False, labels=['', ''], colors=colors)
# flatten the array of axes
axes = axes.flat
# extract the figure object
fig = axes[0].get_figure()
# rotate the pclass label
for ax in axes:
yl = ax.get_ylabel()
ax.set_ylabel(yl, rotation=0, fontsize=12)
# create the legend
legend_elements = [Patch(fc=c, label=l) for c, l in zip(colors, labels)]
fig.legend(handles=legend_elements, loc=9, fontsize=12, ncol=2, borderaxespad=0, bbox_to_anchor=(0., 0.8, 1, .102), frameon=False)
fig.tight_layout()
fig.suptitle('pclass survival', fontsize=15)
axes = ct.plot(kind='pie', autopct='%.1f%%', subplots=True, figsize=(12, 5), labels=["not survived", "survived"])