0

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'.

Link to image

AugLe
  • 113
  • 5
  • Did you try the answer provided on the link of MWE you shared? – Sheldore Sep 11 '18 at 20:53
  • I did. In fact, I've copied the wrong code in my question (I've edited to reflect the code that I tried). I've been thinking of combining the code in the answer and in the question to have 3 stacked histogram in 1 plot, but ended up having 3 overlapped each other. Is there a way to spread them along the x-axis – AugLe Sep 12 '18 at 08:20
  • Right now it is not clear how your final figure should look like. It is always best to show some sample desired output figure so that people can provide more concrete solution rather than editing their solutions again and again – Sheldore Sep 12 '18 at 09:00
  • I have edited the question with a like to an expected look of the plot – AugLe Sep 12 '18 at 09:23

0 Answers0