matplotlib 如何通过填充正确地模糊多个重叠地块?

vbopmzt1  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(133)

我有一个数字,我希望在每个情节下填充,以掩盖背后的情节。
我想要的结果类似于下面的例子:

我想我需要设置plotfill_between(或两者都是?)的zorder,但我似乎无法得到正确的组合。
我目前的情节和代码如下。

我的当前代码:

import numpy as np
import matplotlib.pyplot as plt

def gaussian(x, mu, sig):
    return np.exp(-(x - mu)*(x - mu) / (2 * sig*sig))

mus = [4, 3, 2, 1, 0, -1, -2, -3, -4]
x = np.linspace(-10, 10, 500)

for i in range(len(mus) - 1, -1, -1):
    mu = mus[i]
    y = gaussian(x, mu, 1) + i * 0.1
    plt.plot(x, y)
    plt.fill_between(x, y, 0, color="lightgray")  # The plot lines are not hidden by the fill. Probably need to do something with zorder

plt.show()
3mpgtkmj

3mpgtkmj1#

当然,我问过之后就能找到答案

for i in range(len(mus) - 1, -1, -1):
    mu = mus[i]
    y = gaussian(x, mu, 1) + i * 0.1
    zorder = len(mus) - i #zorder increases as I draw the plots "in front"
    plt.plot(x, y, zorder=zorder)
    plt.fill_between(x, y, 0, color="lightgray", zorder=zorder)

相关问题