Matplotlib.pyplot.fill_betweenx() in Python - GeeksforGeeks

06 Oct, 2021
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. 
The matplotlib.pyplot.fill_betweenx() is used to fill area between two vertical curves. Two points (x1, y) and (x2, y) define the curves. this creates one or more polygons describing the filled areas. The ‘where’ parameter can be used to selectively fill some areas. By default, edges connect the given points directly. The ‘step’ parameter is used, if the filling needs to be a step function. 
Syntax: matplotlib.pyplot.fill_betweenx(y, x1, x2=0, where=None, step=None, interpolate=False, *, data=None, **kwargs)Parameters:  
y: It is array of length N. These are the y coordinates of the nodes that define the curves.  
x1:It is an array of length N or a scalar. This represents the x coordinates of the nodes that define the first curve.  
x2: It is an array of length N and is optional in nature. Its default value is 0. This represents the x coordinates of the nodes that define the second curve.  
where: it is an array of boolean values of length N. It is defined if there is a need to exclude some vertical regions from being filled. It is important to note that this definition means that an isolated true value in between two two false values is where it will not do the filling. Adjacent False values results in not filling both sides of the True value.  
interpolate: It is an optional parameter that accepts boolean values. It is only relevant if where is used and two curves are crossing each other. Semantically where if generally used for x1>x2 or similar cases. By default the filled regions will be placed at the y-array positions defining a filled polygonal area. The section of y that has the intersection are simply clipped. Setting this parameter to True results in calculation of the actual point of intersection and extends to the filled regions till the points.  
step: This is an optional parameter that accepts one of the three values namely, ‘pre’, ‘post’ and ‘mid’. This is used to specify where the steps will occur. pre: From every x position the y value is continued constantlyto the left, ie, the interval (x[i-1], x[i]) has the value y[i].  post:From every x position the y value is continued constantly to the right, ie, the interval (x[i], x[i+1]) has the value y[i].  mid: Half way between the x positions these steps occur.  
pre: From every x position the y value is continued constantlyto the left, ie, the interval (x[i-1], x[i]) has the value y[i].  
post:From every x position the y value is continued constantly to the right, ie, the interval (x[i], x[i+1]) has the value y[i].  
mid: Half way between the x positions these steps occur.  
Returns: It returns a plotted polygon from the PolyCollection. 
other Parameters: **kwargs contains keywords from PolyCollection that controls the polygon properties;
 
Example 1:  
Python3
import matplotlib.pyplot as pltimport numpy as np a = np.linspace(0,2*3.14,50)b = np.sin(a) plt.fill_betweenx(a, b, 0,                  where = (a > -0.5) & (a <= 1),                  color='g') plt.plot(a, b)
Output: 
Example 2:  
Python3
import pylab as pltimport numpy as np N  = np.linspace(0,3,200)A1 = N**2 + 3A2 = np.exp(N) + 2A3 = np.cos(N) plt.plot(N, A1, lw = 3)plt.plot(N, A2, lw = 3)plt.plot(N, A3, lw = 3) plt.fill_betweenx(N, A1, A2,                  color = 'r',                  alpha = .5)plt.fill_betweenx(N, A1, A3,                  color = 'g',                  alpha = .5) plt.show()
Output: 
 
gabaa406
Python-matplotlib
Python
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Convert integer to string in Python
Convert string to integer in Python
Python infinity
How to set input type date in dd-mm-yyyy format using HTML ?
Matplotlib.pyplot.title() in Python