首页 > 解决方案 > 为多个元素创建具有均值和标准差的直方图

问题描述

下面的数组由以下结构组成:

大批:

[[[70, 2.23606797749979], [66, 5.477225575051661], [71, 1.4142135623730951], [75, 4.58257569495584], [68, 0.0]], [[78, 5.196152422706632], [69, 2.0], [69, 2.0], [69, 2.0], [69, 2.0]]]

[[[Average, Standard deviation],[Average, Standard deviation]],[ ... ]]] --> Block

我正在尝试生成一个输出,matplotlib其中图形是具有以下结构的直方图:

在此处输入图像描述

1 个带有平均值的条形图,旁边一个带有标准偏差的条形图,按照此顺序直到块结束,由 5 个均值和 5 个标准差组成

我测试了类似的东西:

x = [[Mean], [Standard Deviation]]
colors = ['red', 'lime']
plt.hist(x, n_bins, density=True, histtype='bar', color=colors, label=colors)

注意:具有平均值和标准差的子列表的数量与日期集的大小有关,示例数组由使用此数据的函数生成

Y轴应该在0到100的范围内,不会经常重复值

标签: pythonmatplotlib

解决方案


我会做:

fig, ax = plt.subplots(figsize=(10,6))
for i in range(2):
    data = a[i]
    x=np.arange(len(data)) + i*6
    # draw means
    ax.bar(x-0.2, data[:,0], color='C0', width=0.4)

    # draw std
    ax.bar(x+0.2, data[:,1], color='C1', width=0.4)

# separation line
ax.axvline(4.75)

# turn off xticks
ax.set_xticks([]);

输出:

在此处输入图像描述


推荐阅读