【问题标题】:Stacked histogram with bin heights defined in numpy array在 numpy 数组中定义了 bin 高度的堆叠直方图
【发布时间】:2015-04-18 10:27:00
【问题描述】:

我想从 numpy 数组中创建一个堆叠直方图,其中的条目是所需的 bin 高度。

例如,我有 12 个 bin 定义为:

bins = np.linspace(0,120,13)

我有几个 numpy 数组,每个数组有 12 个元素(每个 bin 1 个):

hist1 = [1.1,2.2,3.3,4.4,......,hist1[11]]
hist2 = [9.9,8.8,7.7,6.6,......,hist2[11]]

我想变成堆叠直方图。 matplotlib 可以做到这一点吗?

【问题讨论】:

标签: python numpy matplotlib


【解决方案1】:

权重参数将为您解决这个问题。在每个桶中放一个数据点,然后通过指定其权重来控制高度

import numpy as np
import matplotlib.pyplot as plt

bins = np.linspace(0,120,13)


# my approximation of your example arrays
hist1 = np.arange(1.1,15, 1.1)
hist2 = np.arange(15, 1.1, -1.1)

all_weights = np.vstack([hist1, hist2]).transpose()
all_data = np.vstack([bins, bins]).transpose()
num_bins = len(bins)

plt.hist(x = all_data, bins = num_bins, weights = all_weights, stacked=True, align = 'mid', rwidth = .5)

如果您的垃圾箱间隔不均,它们的厚度会开始不同,但除此之外,这应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 2016-12-30
    • 2020-09-21
    • 2016-02-07
    • 1970-01-01
    • 2016-02-13
    • 2018-06-05
    • 2018-06-16
    • 2017-12-07
    • 2014-03-12
    相关资源
    最近更新 更多