I have the following code to draw some histograms about subjects in a database:
import matplotlib.pyplot as plt
attr_info = {
'Gender': ['m', 'f', 'm', 'm', 'f', 'm', 'm', 'f', 'm', 'f'],
'Age': [9, 43, 234, 23, 2, 95, 32, 63, 58, 42],
'Smoker': ['y', 'n', 'y', 'y', 'n', 'n', 'n', 'n', 'y', 'y']
}
bin_info = {key: None for key in attr_info}
bin_info['Age'] = 10
for name, a_info in attr_info.items():
plt.figure(num=name)
counts, bins, _ = plt.hist(a_info, bins=bin_info[name], color='blue', edgecolor='black')
plt.margins(0)
plt.title(name)
plt.xlabel(name)
plt.ylabel("# Subjects")
plt.yticks(range(0, 11, 2))
plt.grid(axis='y')
plt.tight_layout(pad=0)
plt.show()
This code works but it draws each attribute's distribution in a separate histogram. What I'd like to achieve is something like this:

I'm aware plt.hist has a stacked parameter, but that seems to be intended for a slightly different use, where you're stacking the same attributes on each other at different subject types. You could for example draw a histogram where each whole bar would represent some age range and the bar itself would be a stack of smokers in one colour and non-smokers in another.
I haven't been able to figure out how to use it to stack (and properly label as in the image) different attributes on top of each other in each bar.

