如何在matplotlib中使用可变颜色alpha对曲线下着色?

2024-06-25 23:59:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我希望fill_between的alpha值可以根据概率分布进行设置,从而得到一个绘图like in this question about filling under a curve,,但取而代之的是,根据高斯分布,在上面和下面都用alpha进行着色。在

我想也许有一些方法可以在填充之间进行修改,但到目前为止,我还无法找到答案。这是我到目前为止的情况,谁能做得更优雅些?在

# 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


Tags: to数据inalphadatanpbetweenax