【问题标题】:Matplotlib histogram with highlight带突出显示的 Matplotlib 直方图
【发布时间】:2020-09-23 18:53:16
【问题描述】:

我想在 python 的 matplotlib 中创建一个直方图,其(彩色)突出显示低于某个阈值(不一定与 bin 的边缘重合)。如何用蓝色绘制橙色线下方的直方图?

请查看所需示例:

【问题讨论】:

标签: matplotlib


【解决方案1】:

让我们再次尝试在直方图上绘制条形图:

samples = np.random.normal(0,100,10000)

hist, bins = np.histogram(samples, bins=20)

thresh = -200

mask = bins < thresh
below_thresh = np.array(bins[mask].tolist() + [thresh])

plt.figure(figsize=(10,6))

# original histogram
plt.bar(bins[:-1],hist, width=np.diff(bins), color='C0', align='edge');

# below threshold
plt.bar(below_thresh[:-1], hist[mask[:-1]], 
        width=np.diff(below_thresh), color='C1',
        align='edge')

输出:

【讨论】:

    猜你喜欢
    • 2016-08-29
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 2017-08-09
    • 2019-07-11
    • 2017-04-28
    相关资源
    最近更新 更多