首页 > 解决方案 > 透明的“覆盖”matplotlib stackplot

问题描述

我有一个简单的堆栈图:

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 使红色区域透明。

在此处输入图像描述

标签: pythonpython-3.xmatplotlibarea

解决方案


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

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

在此处输入图像描述


推荐阅读