Creating Grouped bars in Matplotlib
Question: I have just started learning python and I am using the Titanic data set to
practice

I am not able to create a grouped bar chart and it it giving me an error
'incompatible sizes: argument 'height' must be length 2 or scalar'

    
    
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    df = pd.read_csv("Titanic/train.csv")
    
    top_five = df.head(5)
    print(top_five)
    column_no = df.columns
    print(column_no)
    
    female_count = len([p for p in df["Sex"] if p == 'female'])
    male_count = len([i for i in df["Sex"] if i == 'male'])
    
    have_survived= len([m for m in df["Survived"] if m == 1])
    not_survived = len([n for n in df["Survived"] if n == 0])
    
    
    plt.bar([0],female_count, color ='b')
    plt.bar([1],male_count,color = 'y')
    plt.xticks([0+0.2,1+0.2],['females','males'])
    plt.show()
    
    plt.bar([0],not_survived, color ='r')
    plt.bar([1],have_survived, color ='g')
    plt.xticks([0+0.2,1+0.2],['not_survived','have_survived'])
    plt.show()
    

it works fine until here and I get two individual charts

Instead i want one chart that displays bars for male and female and color code
the bars based on survival.

This does not seem to work

    
    
    N = 2
    index = np.arange(N)
    bar_width = 0.35
    
    plt.bar(index, have_survived, bar_width, color ='b')
    plt.bar(index + bar_width, not_survived, bar_width,color ='r',)
    
    plt.xticks([0+0.2,1+0.2],['females','males'])
    plt.legend()
    

Thanks in advance!!


Answer: How about replacing your second block of code (the one that returns a
`ValueError`) with this

    
    
    bar_width = 0.35
    tot_people_count = (female_count + male_count) * 1.0
    
    plt.bar(0, female_count, bar_width, color ='b')
    plt.bar(1, male_count, bar_width, color ='y',)
    
    plt.bar(0, have_survived/tot_people_count*female_count, bar_width, color='r')
    plt.bar(1, have_survived/tot_people_count*male_count, bar_width, color='g')
    
    plt.xticks([0+0.2,1+0.2],['females','males'])
    
    plt.legend(['female deceased', 'male deceased', 'female survivors', 'male survivors'],
               loc='best')
    

I get this bar graph as an output,

[![enter image description
here](http://i.stack.imgur.com/PiiNu.png)](http://i.stack.imgur.com/PiiNu.png)

The reason for the error you get is that the `left` and `height` parameters of
`plt.bar` must either have the same length as each other or one (or both) of
them must be a scalar. That is why changing `index` in your code to the simple
scalars `0` and `1` fixes the error.

