I would like to create similar graph with this one (groups of stacked bar chart):
import numpy as np
import matplotlib.pyplot as plt
N = 5
menMeans = (20, 35, 30, 35, 27)
womenMeans = (25, 32, 34, 20, 25)
menStd = (2, 3, 4, 1, 2)
womenStd = (3, 5, 2, 3, 3)
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence
p1 = plt.bar(ind, menMeans, width, yerr=menStd)
p2 = plt.bar(ind, womenMeans, width,
bottom=menMeans, yerr=womenStd)
plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Men', 'Women'))
plt.show()
It's just that instead of each stacked bar per group, it would be each stacked histogram per group. So roughly speaking, I would want to change that plt.bar
into plt.hist
and also keeping the graph layout unchanged.
That way, it would be like putting multiple (same range/bin) histograms into one plot of same axis (not multiple subplot figures), but not sure if it's could be helped if it is thought as a big histogram containing multiple sub-histogram. Which way is easier to approach?
I am using the code here as an MWE, maybe you can help me replicate the same into 3 groups (one the same axis), assuming they are 3 different histograms. I don't care about showing the values of the histograms on the x-axis, just the groups' name would be enough.
x1 = mu + sigma*np.random.randn(990,1)
x2 = mu + sigma*np.random.randn(980,1)
x3 = mu + sigma*np.random.randn(1000,1)
#Stack the data
plt.figure()
plt.hist([x1,x2,x3], bins, stacked=True, normed = True)
plt.show()
I am including a sample plot similar to what I am expecting. To plot this, I have manual shifted the range of 2nd and 3rd histograms (originally they are of the same range as the first one), so the shown x-axis is a bit confusing. If this is the only (easiest) way to do it, I would like to replace the x-axis values to the group name, e.g. 'group 1', 'group 2', 'group 3'.