My code:
import matplotlib.pyplot as plt
import numpy as np
# x > 0
x = np.linspace(0,17, 100)
#x2>=0
y0 = (x*0)
#-x1+x2 <= 1
y1 = 1+x
#x1+6x2 <= 15
y2 = 15/6 - (1/6)*x
#4x1-x2 <= 10
y3 = 4*x-10
plt.xlabel(r'$x_2>=0$')
plt.ylabel(r'$x_1>=0$')
plt.plot(x,y0,'r')
plt.plot(x,y1, 'b')
plt.plot(x,y2, 'y')
plt.plot(x,y3, 'g')
plt.xlim((0,17))
plt.ylim((0,9))
#feasible region
a1 = np.minimum(y2,y3)
a2 = np.minimum(y1,y2)
plt.fill_between(x, y1, y3, where = a1 < a2, color = 'grey', alpha = 0.5)
this generates the following plot:
However, I do not want the grey to extend past the yellow line, i.e. I want to remove the grey that is located inside the triangle above the 4 sided polygon. Meaning, I want the values that exceed above the yellow line to be removed as well, but I am not sure how to represent this without adding another argument to where
. I have only gotten errors when attempting this. Is there a way to specify multiple arguments for where
?
Edit: I have solved the problem by tweaking my parameters to:
#feasible region
a1 = np.maximum(y0,y3)
a2 = np.minimum(y1,y2)
plt.fill_between(x, a1, a2, where = a1 < a2, color = 'grey', alpha = 0.5)
however, I am still curious about potentially specifying multiple arguments to where
, so I will leave the question up.
You can use np.maxiumum
and np.minimum
to define your y-values. This way you don't have to "piece-wise" it. Use maximum
on your lower bounds to define just one lower bound to plot. Same with minimum
and your upper bounds. Also, in your example, you cut off the y axis, where you there is actually shaded area below.
Y1 = np.maximum(y0, y3)
Y2 = np.minimum(y1, y2)
plt.fill_between(x, Y1, Y2, where = Y1 < Y2, color = 'grey', alpha = 0.5)