简体   繁体   English

如何通过填充正确遮盖多个重叠图?

[英]How do I correctly obscure multiple overlapping plots by fill?

I have a figure where I wish to fill under each plot to obscure the plots behind it.我有一个数字,我希望在每个 plot 下填充以掩盖其背后的情节。

My desired result is akin to this example:我想要的结果类似于这个例子:

图,其中每个图都遮盖了每个其他图,其中另一个图小于图。

I think I need to set the zorder of either plot or fill_between (or both?), but I can't seem to get the correct combinations.我想我需要设置zorderplot (或两者?)的fill_between ,但我似乎无法获得正确的组合。

My current plot and code are below.我当前的 plot 和代码如下。

My current code:我当前的代码:

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()

and of course, I find the answer after I ask...当然,我在问完之后找到了答案......

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)  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM