0

I'm trying to compare the number of survivors with their age.

0 - Child

1 - Young

2 - Adult

3 - Middle age

4 - Senior

But my bar chart doesn't show me the number of "Young" survivors. I just can't see it. Dataset is from kaggle: https://www.kaggle.com/c/titanic/data

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

data = pd.read_csv('titanic_data/train.csv')

child = data[data['Age']==0]['Survived'].value_counts()
young = data[data['Age']==1]['Survived'].value_counts()
adult = data[data['Age']==2]['Survived'].value_counts()
mid_age = data[data['Age']==3]['Survived'].value_counts()
senior = data[data['Age']==4]['Survived'].value_counts()

plt.bar(child.index, child, width=0.4, label='Child')
plt.bar(young.index, young, width=0.4, label='Young')
plt.bar(adult.index, adult, width=-0.4, label='Adult')
plt.bar(mid_age.index, mid_age, width=0.4, label='Mid-age')
plt.bar(senior.index, senior, width=0.4, label='Senior')

plt.xticks(np.arange(2), ('Victims', 'Survivors'), rotation=0)
plt.legend()

graph

data[data['Age']==1]['Survived'].value_counts()

0    144
1     75
Name: Survived, dtype: int64

Any solutions how to show this informations on graph?

I also tried this:

child = data[data['Age']==0]['Survived'].value_counts()
young = data[data['Age']==1]['Survived'].value_counts()
adult = data[data['Age']==2]['Survived'].value_counts()
mid_age = data[data['Age']==3]['Survived'].value_counts()
senior = data[data['Age']==4]['Survived'].value_counts()

plt.bar(child.index, child, align='edge', width=0.4, label='Child')
plt.bar(young.index, young, align='edge', width=0.4, label='Young')
plt.bar(adult.index, adult, align='edge', width=-0.4, label='Adult')
plt.bar(mid_age.index, mid_age, align='edge', width=0.4, label='Mid-age')
plt.bar(senior.index, senior, align='edge', width=0.4, label='Senior')

plt.xticks(np.arange(2), ('Victims', 'Survivors'), rotation=0)
plt.legend()

But the result is ugly:

graph2

I tried to understand how to implement this solution: Python matplotlib multiple bars but I just can't figure out how to do it.

EDIT: I did it in this way:

child = data[data['Age']==0]['Survived'].value_counts()
young = data[data['Age']==1]['Survived'].value_counts()
adult = data[data['Age']==2]['Survived'].value_counts()
mid_age = data[data['Age']==3]['Survived'].value_counts()
senior = data[data['Age']==4]['Survived'].value_counts()

y_pos = np.arange(len(child))

plt.bar(child.index-0.14, child, width=0.14, label='Child')
plt.bar(young.index+0.14, young, width=0.14, label='Young')
plt.bar(adult.index+0.28, adult, width=-0.14, label='Adult')
plt.bar(mid_age.index, mid_age, width=0.14, label='Mid-age')
plt.bar(senior.index-0.28, senior, width=0.14, label='Senior')

plt.xticks(np.arange(2), ('Victims', 'Survivors'), rotation=0)
plt.legend()
Eliro
  • 223
  • 2
  • 4
  • 12
  • Maybe the number of young Survivor is less so the bar is hidden behind the other bars – Sheldore Jul 20 '19 at 13:32
  • So how to show this informations on graph? Is there better way? – Eliro Jul 20 '19 at 13:52
  • 1
    Check [this](https://stackoverflow.com/questions/14270391/python-matplotlib-multiple-bars) out. For more help, provide a complete runnable code. – Sheldore Jul 20 '19 at 14:01
  • But this is full and runnable code. And I don't understand most of the code from link. – Eliro Jul 21 '19 at 08:06
  • Possible duplicate of [Python matplotlib multiple bars](https://stackoverflow.com/questions/14270391/python-matplotlib-multiple-bars) – M_S_N Jul 21 '19 at 08:40
  • RizaGul, I don't know how to implement this solution to my graph and dataset. – Eliro Jul 21 '19 at 08:43
  • The problem is that your bars are not stacked. Check the answer on this post https://stackoverflow.com/questions/54970170/stacked-bar-plot-from-loop-not-adding-different-components-of-bars/54971501#54971501 – mortysporty Jul 21 '19 at 18:52

0 Answers0