透明的“重叠” matplotlib堆栈图

时间:2019-02-05 15:15:35

标签: python python-3.x matplotlib area

我有一个简单的堆栈图:

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

1 个答案:

答案 0 :(得分:1)

如果我的理解正确,您是否需要以下类似内容?

在这种情况下,您可以指定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