如何在matplotlib的曲线下遮阴,但是有可变的颜色alpha?

时间:2017-09-02 18:09:23

标签: python matplotlib visualization uncertainty

我有一些数据点是一个变量的函数。我想绘制这些,但每个数据都存在相关的不确定性。错误栏可以,但我希望能够可视化我们期望错误分发的方式。例如,可以给出具有已知宽度的高斯分布。

我希望fill_between的alpha值可以根据概率分布进行设置,从而产生一个情节like in this question about filling under a curve,,但是根据一个高斯分别用alpha表示上下两个阴影。

我想可能有一些方法来破解fill_between以使其工作,但到目前为止我无法弄明白。这是我到目前为止所做的,任何人都可以更优雅地做到这一点吗?

# example x data, y data, and uncertainties
def exampleFunc(x):
    return np.sin((x/1.5-3.0)**2)+1.0

xdata = np.linspace(0,10,100)
ydata = exampleFunc(xdata)

# define this data to be gaussian distributed with these standard 
# deviations
uncertainties = np.sqrt(ydata)

fig, ax = pl.subplots()


# plot the data centers on a line
ax.plot(xdata, ydata, 'b') # blue to stand out from shading

numsigma = 5 # how many standard deviations to go out
numsteps = 100 # how many steps to take in shading

# go to shade the uncertainties between, out to 4 sigma
for i in range(1,numsteps+1):
    top = ydata + uncertainties/numsteps*i*numsigma
    bottom = ydata - uncertainties/numsteps*i*numsigma
    ax.fill_between(xdata, bottom, top, color='r', 
        alpha=1.0/numsteps)

plot with shaded representation of uncertainty

0 个答案:

没有答案