matplotlib - 如何使用 matplotlib 在两条直方图线之间完成颜色填充?

标签 matplotlib histogram

我有两个直方图(信号和背景),每个直方图都有不确定性上限和下限。我想将每个直方图绘制为仅使用没有填充的彩色边缘,然后用它们自己的没有填充的彩色边缘绘制上部和下部不确定性直方图,然后填充上部和下部直方图之间的区域,如一种低调的乐队。如何实现这一目标?

这是我刚才的非常粗略的代码(可以在 Jupyter 中使用):

import matplotlib.pyplot as plt
import numpy as np
from pylab import normal
import seaborn as sns
sns.set(context="paper", font="monospace")
import warnings
warnings.filterwarnings('ignore')

%matplotlib inline
plt.rcParams["figure.figsize"] = [8, 8]
plt.rc("text", usetex=True)
plt.rc("font", family="serif")

s    = normal(1, .2, 5000)
s_up = normal(1, .2, 5500)
s_dn = normal(1, .2, 4600)
b    = normal(2, .2, 2500)

ns, bins, patches = plt.hist(
    [s, s_up, s_dn, b],
    color=['r', 'g', 'g', 'b'],
    label=['signal nominal', 'signal upper bound', 'signal lower bound', 'background'],
    alpha=0.5,
    linewidth=1,
    histtype='stepfilled');
plt.setp(patches[0], edgecolor='r')
plt.setp(patches[1], edgecolor='b')
plt.legend();
plt.show();

最佳答案

类似的事情?

s    = normal(1, .2, 5000)
s_up = normal(1, .2, 5500)
s_dn = normal(1, .2, 4600)

bins = np.linspace(0., 2., 21)

n_up,b_up,p_up = plt.hist(s_up, bins=bins,  bottom=0, linewidth=1, histtype='stepfilled', facecolor='none', edgecolor='red', linestyle='--', label='signal upper bound')
n_dn,b_dn,p_dn = plt.hist(s_dn, bins=bins,  bottom=0, linewidth=1, histtype='stepfilled', facecolor='none', edgecolor='green', linestyle='--', label='signal lower bound')
n,b,p = plt.hist(s, bins=bins, bottom=0, linewidth=2, histtype='stepfilled', facecolor='none', edgecolor='k', label='signal nominal')
plt.bar(x=b_up[:-1], height=n_up-n_dn, bottom=n_dn, width=np.diff(b_up), align='edge', linewidth=0, color='red', alpha=0.25, zorder=-1, label='uncertainty band')
plt.legend()

enter image description here

关于matplotlib - 如何使用 matplotlib 在两条直方图线之间完成颜色填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57400914/

相关文章:

r - 使用直方图作为 R 中的输入

python - 为什么我的代码只写最后一个变量?

python - 当时间数据已作为索引确定时,如何绘制时间相关数据?

python - 如何从图像中提取 RGB 并仅将 RG 绘制为图形? R代表X,G代表Y

java - 如何在 Java 中添加 vector 作为值

Python:如何制作具有相同*大小* bin 的直方图

python - pandas python 中一个字段与另一个字段的直方图

python - Matplotlib,如何将数组表示为图像?

python - Python 中的 ODE 求解器出错

r - ggplot2 直方图显示按 bin 而不是计数分组的比例