python - Creating Grouped bars in Matplotlib -


i have started learning python , using titanic data set practice

i not able create grouped bar chart , it giving me error 'incompatible sizes: argument 'height' must length 2 or scalar'

import numpy np import pandas pd import matplotlib.pyplot 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 p in df["sex"] if p == 'female']) male_count = len([i in df["sex"] if == 'male'])  have_survived= len([m m in df["survived"] if m == 1]) not_survived = len([n 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 , 2 individual charts

instead want 1 chart displays bars male , female , color code bars based on survival.

this not seem 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!!

how replacing second block of code (the 1 returns valueerror) 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 bar graph output,

enter image description here

the reason error left , height parameters of plt.bar must either have same length each other or 1 (or both) of them must scalar. why changing index in code simple scalars 0 , 1 fixes error.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -