Make filled polygons between two curves in Python using Matplotlib

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.
To create filled polygons between two curves a PolyCollection filling needs to be created between y1 and y2.
Parameters for the above task:
- x: It is an array of length N that holds data of x.
- y1: It is an array or a scalar of length N that holds data of y.
- y2:It is an array or a scalar of length N that holds data of y.
Example:
Python3
import matplotlib.pyplot as pltimport numpy as np# set the width width = 3.5# set the heightheight = 2.5# set the depthdepth = 65# plot the figureplt.figure(figsize =(width, height), dpi = depth)# set the x array of length 3x = [1, 3, 6]# set y1 array of length 3y1 = [2, 3.5, 4]# set y2 array of length 3y2 = [3, 4, 5.5]# fill the horizontal area between y1 and y2plt.fill_between(x, y1, y2)# show the plotted figureplt.show() |
Output:




