0

Hi Matplotlib and Seaborn users,

I have a histogram and would like to include the % proportion on each bar. Below is an example of the code I have so far

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

data = {
'gender': ['male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'male', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female', 'female'],
'height': [6, 4, 10, 5, 2, 6, 4, 1, 1, 10, 4, 9, 9, 2, 9, 6, 6, 8, 3, 4, 3, 3, 2, 3, 8, 9, 7, 10, 4, 8, 5, 7, 6, 1, 9, 6, 9, 5, 4, 6, 2, 6, 5, 6, 5, 1, 9, 1, 7, 4]
}

df = pd.DataFrame(data)
df.head(n = 5)

# defining the scale and the categories
myBin = df['gender']
myScale = df['height']

#Seperate the 2 categories using boolean for each category
myheight1 = myBin == 'male'
myheight2 = myBin == 'female'

# plotting the overlaying histogram
plt.figure(figsize = (8, 6))
plt.hist(myheight1height, edgecolor = 'blue', alpha = 0.5, bins = 10, label = 'Male')
plt.hist(myheight2height, edgecolor = 'blue', alpha = 0.5, bins = 10, label = 'Female')
plt.legend(loc = 'upper right')
plt.xlabel('Gender')
plt.ylabel('height')
plt.show()

Thanks in advance!

Stephen Okiya
  • 315
  • 1
  • 8
  • I see the question is closed but there's a difference with the solution you provide the link. Please my case is a super-imposed histogram. – Stephen Okiya Apr 27 '22 at 10:21
  • Your code doesn't run (i.e. `NameError: name 'myheight1height' is not defined`). Perhaps if it did @jezrael could see what you are producing is different. – DarrylG Apr 27 '22 at 10:29
  • @DarryIG My sincere apologies. I have edited the typo. The code should now run – Stephen Okiya Apr 28 '22 at 17:35

0 Answers0