Matplotlib.axes.Axes.fill_between() in Python - GeeksforGeeks

13 Apr, 2020
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library.
The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
The Axes.fill_between() function in axes module of matplotlib library is used to fill the area between two horizontal curves.
Syntax: Axes.fill_between(self, x, y1, y2=0, where=None, interpolate=False, step=None, *, data=None, **kwargs)
Parameters: This method accept the following parameters that are described below:
x: This parameter contains the horizontal coordinates of the data points which are used to define the curves.
y1: This parameter contains the y coordinates of the data points which are used to define the first curves
y2: This parameter contains the y coordinates of the data points which are used to define the second curves. It is an optional with default value of 0.
where: This parameter is an optional parameter. And it is used to exclude some horizontal regions from being filled.
interpolate: This parameter is also an optional parameter. And it is the linewidth of the errorbar lines with default value NONE.
step: This parameter is also an optional parameter. And it is used to define if the filling should be a step function or not.
Returns: This returns a PolyCollection containing the plotted polygons.
Below examples illustrate the matplotlib.axes.Axes.fill_between() function in matplotlib.axes:
Example-1:
# Implementation of matplotlib function      import numpy as npimport matplotlib.pyplot as plt  x = np.arange(-5, 5, 0.01)y1 = -3 * x*x + 10 * x + 10y2 = 3 * x*x + x  fig, ax = plt.subplots()ax.plot(x, y1, x, y2, color ='black')ax.fill_between(x, y1, y2, where = y2 >y1,                 facecolor ='green', alpha = 0.8)  ax.fill_between(x, y1, y2, where = y2 <= y1,                facecolor ='black', alpha = 0.8)  ax.set_title('matplotlib.axes.Axes.fill_between Example1')plt.show()
Output:
Example-2:
# Implementation of matplotlib function      import numpy as npimport matplotlib.pyplot as plt  x = np.arange(0.0, 2, 0.01)y1 = np.sin(2 * np.pi * x)y2 = 0.8 * np.sin(4 * np.pi * x)  fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1,                                         sharex = True,                                         figsize =(6, 6))  ax1.fill_between(x, y1, facecolor ='green')ax1.set_title('Fill_Between y1 and 0')  ax2.fill_between(x, y1, 1, facecolor ='green')ax2.set_title('Fill_Between y1 and 1')  ax3.fill_between(x, y1, y2, facecolor ='green')ax3.set_title('Fill_Between y1 and y2')ax3.set_xlabel('x')fig.tight_layout()  ax4.fill_between(x, y1, y2, where = y2 <= y1,                 facecolor ='green')  ax4.set_title('Fill_Between y1 and y2 with condition y2 <= y1 ' )ax4.set_xlabel('x')fig.tight_layout()  plt.show()
Output:
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
How to Install PIP on Windows ?
Read a file line by line in Python
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
Reading and Writing to text files in Python
*args and **kwargs in Python
Convert integer to string in Python