Home > front end >  Fill in feasible region with Fill_between
Fill in feasible region with Fill_between

Time:10-12

I'm trying to fill in the maximum outward region as attached in the image. Below code will generate the line graph, however, I'm struggling to fill in the region as in the image.enter image description here

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$')

CodePudding user response:

You could fill the entire background with a color, then use fill_between make the area below the lines white.

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');

enter image description here

CodePudding user response:

The easiest way, would be to use the maximum of the 3 curves:


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()

fill_between using max

  • Related