Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The goal here is to create a double histogram with string labels which is not supported yet with the hist() matplotlib function
- import matplotlib.pyplot as plt
- import numpy as np
- from collections import Counter
- # Dead list
- l1 = titanic[titanic["Survived"] == 0]["Embarked"].dropna()
- # Survivor list
- l2 = titanic[titanic["Survived"] == 1]["Embarked"].dropna()
- # draw histogram function
- def hist2Str(_l1_Values, _l2_Values, _l1_Label, _l2_Label, _X_Label, _Y_Label):
- step = 0.3
- # Values lists 1 & 2
- count1 = Counter(_l1_Values)
- counts1 = count1.values()
- count2 = Counter(_l2_Values)
- counts2 = count2.values()
- # Labels in x
- labels = count1.keys()
- # Draw histogram
- bar_x = np.arange(len(counts1))
- plt.bar(bar_x, counts1, step, align = 'center')
- plt.bar(bar_x + step, counts2, step, color='r', align = 'center')
- plt.xticks(bar_x + step/2, labels)
- # Axis labels & draw
- plt.ylabel(_Y_Label)
- plt.xlabel(_X_Label)
- plt.legend([_l1_Label, _l2_Label])
- plt.draw()
- hist2Str(l1, l2, "Dead", "Survived", "Embarked", "Passenger")
Add Comment
Please, Sign In to add comment