我正在嘗試填充影像中附加的最大向外區域。下面的代碼將生成折線圖,但是,我正在努力填充影像中的區域。
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0,20,2000)
y1 = (-2*x) 6
y2 = 5-x
y3 = (20 - (2*x))/10
plt.plot(x,y1,label = r'$2x1 x2geq6$')
plt.plot(x,y2,label = r'$x ygeq5$')
plt.plot(x,y3,label = r'$2x 10ygeq20$')
plt.xlim((0,10))
plt.ylim((0,10))
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
uj5u.com熱心網友回復:
最簡單的方法是使用 3 條曲線中的最大值:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 20, 2000)
y1 = (-2 * x) 6
y2 = 5 - x
y3 = (20 - (2 * x)) / 10
plt.plot(x, y1, label=r'$2x1 x2geq6$')
plt.plot(x, y2, label=r'$x ygeq5$')
plt.plot(x, y3, label=r'$2x 10ygeq20$')
plt.fill_between(x, np.amax([y1, y2, y3], axis=0), 10, color='black', alpha=0.3)
plt.xlim((0, 10))
plt.ylim((0, 10))
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
plt.show()

uj5u.com熱心網友回復:
您可以用一種顏色填充整個背景,然后使用fill_between將線條下方的區域設為白色。
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['axes.facecolor'] = 'lightgrey'
x = np.linspace(0,20,2000)
y1 = (-2*x) 6
y2 = 5-x
y3 = (20 - (2*x))/10
plt.plot(x,y1,label = r'$2x1 x2geq6$')
plt.plot(x,y2,label = r'$x ygeq5$')
plt.plot(x,y3,label = r'$2x 10ygeq20$')
plt.xlim((0,10))
plt.ylim((0,10))
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
plt.fill_between(x,y3, color='white')
plt.fill_between(x,y2, color='white')
plt.fill_between(x,y1, color='white');

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317820.html
標籤:Python 麻木的 matplotlib 阴谋 线性规划
下一篇:置信區間3維圖
