Python: 使用NumPy数组创建分层直方图,显示不同值的数量

2024-06-16 11:34:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下问题。我有一个7列的数组。我想要一个列1、2和5的堆积柱状图,这样就有3个条形图,每一列都有不同元素的数量。所以对于列1,我有9个4值,1个值3。在第2列中,我有3个值3和7个值4。在第5列中,我有不同数量的值。我有一个方法,但它非常肮脏,我只能画出第1列和第2列。此外,我想在不同的条形图上绘制一个标签,以显示它们显示的数字。在

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()

enter image description here

编辑 应该看起来像:

enter image description here


Tags: 数量abnppltwidthint条形图ind
1条回答
网友
1楼 · 发布于 2024-06-16 11:34:55

首先,你需要将你的列表放入一个2D numpy数组中。如您所知,您有多少列,这很简单:

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

所以现在有了一个包含10行7列的2D数组(shape=(10,7))。所以现在可以用matplotlib轻松地绘制直方图了。在

^{pr2}$

Histogram of columns 1,2 and 5

相关问题 更多 >