简体   繁体   中英

Python : Creating stacked histogram with number of different values in numpy array

I have the followin problem. I have an array with 7 columns. I want a stacked histogram for column 1,2 and 5, so that there are 3 bars that are stacked with the number of different elements in each column. So for column 1 i have 9 values with 4 and 1 value with 3. In column 2 I have 3 values of 3 and 7 vlaues of 4. In column 5 i have different number of values. I have a appoach but its very dirty and i could only plot the column 1 and 2. Furthermore i want to plot a label to the different bars to show what number they are displaying.

bestof10=    [4.041970802919708228e-01  4.000000000000000000e+00    4.000000000000000000e+00    6.710000000000000853e+01    5.500000000000000444e-01    4.000000000000000000e+00    6.000000000000000000e+01
    3.730468750000000000e-01    4.000000000000000000e+00    4.000000000000000000e+00    1.932000000000000171e+02    7.000000000000000666e-01    6.000000000000000000e+00    6.200000000000000000e+01
    3.626570915619389823e-01    4.000000000000000000e+00    3.000000000000000000e+00    3.900000000000000000e+01    5.000000000000000000e-01    4.000000000000000000e+00    5.300000000000000000e+01
    3.568320278503046006e-01    4.000000000000000000e+00    4.000000000000000000e+00    8.640000000000000568e+01    6.000000000000000888e-01    7.000000000000000000e+00    6.300000000000000000e+01
    3.527336860670193808e-01    4.000000000000000000e+00    4.000000000000000000e+00    6.780000000000001137e+01    6.000000000000000888e-01    3.000000000000000000e+00    5.900000000000000000e+01
    3.521825396825397081e-01    4.000000000000000000e+00    4.000000000000000000e+00    1.568000000000000114e+02    7.000000000000000666e-01    1.000000000000000000e+00    5.700000000000000000e+01
    3.432835820895522305e-01    3.000000000000000000e+00    4.000000000000000000e+00    8.904999999999999716e+01    6.500000000000000222e-01    7.000000000000000000e+00    4.200000000000000000e+01
    3.374888691006233121e-01    4.000000000000000000e+00    3.000000000000000000e+00    3.194999999999999929e+01    4.500000000000000111e-01    7.000000000000000000e+00    5.600000000000000000e+01
    3.364879074658254643e-01    4.000000000000000000e+00    4.000000000000000000e+00    2.430000000000000000e+02    7.500000000000000000e-01    5.000000000000000000e+00    6.100000000000000000e+01
    3.244781783681214282e-01    4.000000000000000000e+00    3.000000000000000000e+00    8.100000000000001421e+01    6.000000000000000888e-01    6.000000000000000000e+00    5.500000000000000000e+01]

a  = np.bincount(bestof10[:,1].astype(int))
ab = np.nonzero(a)[0]
a = a[ab]
a = tuple(a)

b  = np.bincount(bestof10[:,2].astype(int))
bb = np.nonzero(b)[0]
b  = b[bb]
b = tuple(b)

histogramParas=np.column_stack((a,b))

N = 2

ind = np.arange(N)
width = 0.35 

p1 = plt.bar(ind, histogramParas[0], width, color='r')
p2 = plt.bar(ind, histogramParas[1], width, color='y',bottom=histogramParas[0])

plt.ylabel('Scores')
plt.title('Scores by group and gender')
plt.xticks(ind+width/2., ('Radius', 'FRC') )

plt.show()

在此处输入图片说明

EDIT Should look like:

在此处输入图片说明

First of all you need to get your list into a 2D numpy array. As you know how many columns you have this is easy:

x = np.array(bestof10)
x = x.reshape(-1, 7)

So now you have a 2D array of 10 rows and 7 columns (shape = (10, 7)). So now you can easily plot your histogram with matplotlib.

fig, ax = plt.subplots()
_ = ax.hist([x[:, 1], x[:, 2], x[:, 5]],
            stacked=True, normed=False)

第1,2和5列的直方图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM