Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Draw 2 histograms with string labels in Python matplotlib
# 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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.