【问题标题】:Python : Creating stacked histogram with number of different values in numpy arrayPython:在numpy数组中创建具有多个不同值的堆叠直方图
【发布时间】:2017-12-07 23:43:09
【问题描述】:

我有以下问题。我有一个 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()

编辑 应该是这样的:

【问题讨论】:

  • 请添加您用于创建给定图像的代码。
  • 我没有创建图像,我只是想展示堆叠条的样子
  • 那么你真的有两个问题:“我如何制作这样的图表?”和“我如何像这样索引这个数组?”第二个答案是np.array(x).reshape(7,-1)[[0,1,4]]。第一个不确定。
  • 查看 matplotlib 的 hist() 函数

标签: python arrays numpy matplotlib scipy


【解决方案1】:

首先,您需要将列表放入 2D numpy 数组中。如您所知,您有多少列,这很容易:

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

所以现在您有一个 10 行和 7 列的二维数组(形状 = (10, 7))。所以现在您可以使用 matplotlib 轻松绘制直方图。

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

【讨论】:

  • 感谢您的建议:) 但我只想要第 1,2 和 5 列中不同元素的数量,就像我编辑中的草图一样。