Guest User

Untitled

a guest
May 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. # The goal here is to create a double histogram with string labels which is not supported yet with the hist() matplotlib function
  2.  
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. from collections import Counter
  6.  
  7. # Dead list
  8. l1 = titanic[titanic["Survived"] == 0]["Embarked"].dropna()
  9. # Survivor list
  10. l2 = titanic[titanic["Survived"] == 1]["Embarked"].dropna()
  11.  
  12. # draw histogram function
  13. def hist2Str(_l1_Values, _l2_Values, _l1_Label, _l2_Label, _X_Label, _Y_Label):
  14. step = 0.3
  15. # Values lists 1 & 2
  16. count1 = Counter(_l1_Values)
  17. counts1 = count1.values()
  18. count2 = Counter(_l2_Values)
  19. counts2 = count2.values()
  20. # Labels in x
  21. labels = count1.keys()
  22.  
  23. # Draw histogram
  24. bar_x = np.arange(len(counts1))
  25. plt.bar(bar_x, counts1, step, align = 'center')
  26. plt.bar(bar_x + step, counts2, step, color='r', align = 'center')
  27. plt.xticks(bar_x + step/2, labels)
  28. # Axis labels & draw
  29. plt.ylabel(_Y_Label)
  30. plt.xlabel(_X_Label)
  31. plt.legend([_l1_Label, _l2_Label])
  32. plt.draw()
  33. hist2Str(l1, l2, "Dead", "Survived", "Embarked", "Passenger")
Add Comment
Please, Sign In to add comment