gpt4 book ai didi

python - 透明 "overlay"matplotlib stackplot

转载 作者:行者123 更新时间:2023-12-01 08:22:27 29 4
gpt4 key购买 nike

我有一个简单的堆栈图:

import matplotlib.pyplot as plt

label, color = ["R", "M"], ["green", "blue"]
X = [x for x in range(101)]
Y1 = [31.4*x for x in range(101)]
Y2 = [26.51*x-250 if (26.51*x-250)>=0 else 0 for x in range(101)]
Y3 = [31.4*x if 31.4*x < 2400 else 2400 for x in range(101)]

plt.stackplot(X, Y1, Y2, labels=label, colors = color)
plt.fill_between(X,Y3,[2400 for x in range(101)], color = 'r')
plt.legend(loc=2)
plt.xlim(0,100)
plt.ylim(0,5600)
plt.show()

可能有更好的方法来显示我想要显示的内容,但我只想知道如果可能的话,如何使用 matplotlib 使红色区域透明。

enter image description here

最佳答案

如果我理解正确的话,你需要类似下面的东西吗?

在这种情况下,您可以指定 fill_ Between 的透明度

import matplotlib.pyplot as plt

label, color = ["R", "M"], ["green", "blue"]
X = [x for x in range(101)]
Y1 = [31.4*x for x in range(101)]
Y2 = [26.51*x-250 if (26.51*x-250)>=0 else 0 for x in range(101)]
Y3 = [31.4*x if 31.4*x < 2400 else 2400 for x in range(101)]

plt.stackplot(X, Y1, Y2, labels=label, colors = color)
plt.fill_between(X,Y3,[2400 for x in range(101)], color = 'r', alpha=0.3)
plt.legend(loc=2)
plt.xlim(0,100)
plt.ylim(0,5600)

enter image description here

关于python - 透明 "overlay"matplotlib stackplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54537539/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com