Thursday, September 4, 2025
HomeLanguagesMake filled polygons between two curves in Python using Matplotlib

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: 
 

  1. x: It is an array of length N that holds data of x. 
     
  2. y1: It is an array or a scalar of length N that holds data of y. 
     
  3. y2:It is an array or a scalar of length N that holds data of y. 
     

Example: 
 

Python3




import matplotlib.pyplot as plt
import numpy as np
# set the width
width = 3.5
 
# set the height
height = 2.5
 
# set the depth
depth = 65
 
# plot the figure
plt.figure(figsize =(width, height), dpi = depth)
 
# set the x array of length 3
x = [1, 3, 6]
 
# set y1 array of length 3
y1 = [2, 3.5, 4]
 
# set y2 array of length 3
y2 = [3, 4, 5.5]
 
# fill the horizontal area between y1 and y2
plt.fill_between(x, y1, y2)
 
# show the plotted figure
plt.show()


Output: 
 

filled polygons between two curves in Python

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS