2 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
您可以在零和三條曲線的最小值之間進(jìn)行填充:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 20, 2000)
y1 = (36 - 2 * x) / 6
y2 = (30 - 5 * x) / 3
y3 = (40 - 8 * x) / 2
plt.plot(x, y1, label=r'$2 x_{1} + 6 x_{2}\leq 36$')
plt.plot(x, y2, label=r'$x_{1} + 3 x_{2}\leq 30$')
plt.plot(x, y3, label=r'$x_{1} + 2 x_{2}\leq 40$')
plt.xlim((0, 16))
plt.ylim((0, 11))
plt.xlabel(r'$x_1$')
plt.ylabel(r'$x_2$')
plt.fill_between(x, y1, y2, color='grey', alpha=0.5,
interpolate=True)
plt.fill_between(x, 0, np.min([y1, y2, y3], axis=0), color='red', alpha=0.5, hatch='//',
interpolate=True, label='$intersection$')
plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.tight_layout()
plt.show()

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
注意:
np.vstack([y1, y2, y3])
從 3 個(gè)“y”數(shù)組創(chuàng)建一個(gè) 3 行數(shù)組。….min(0)
計(jì)算每列的最小值,因此它實(shí)際上是 3 個(gè)源數(shù)組中的最小值(對(duì)于較高的x也具有負(fù)值)。….clip(min=0)
將上述負(fù)數(shù)元素轉(zhuǎn)換為0。
因此,添加到您的代碼中:
plt.fill_between(x, 0, np.vstack([y1, y2, y3]).min(0).clip(min=0), color='yellow', alpha=0.5, interpolate=True)
例如在你的第一個(gè)fill_ Between之后。
對(duì)于您的數(shù)據(jù)以及添加了上述指令的代碼,我得到:
如果需要,請(qǐng)將填充顏色更改為適合您需要的顏色。
添加回答
舉報(bào)