Make filled polygons between two horizontal 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.

Below is the implementation.

filter_none

edit
close

play_arrow

link
brightness_4
code

import matplotlib.pyplot as plt
   
      
x_inp= input("Enter values of X array:  ").split()
x= [int(i) for i in x_inp]
  
y1_inp= input("Enter values of Y1 array:  ").split()
y1= [int(i) for i in y1_inp]
  
y2_inp= input("Enter values of Y2 array:  ").split()
y2= [int(i) for i in y2_inp]
   
color=input("Enter color:  ")
  
plt.fill_between(x, y1, y2, color = color)
plt.show()

chevron_right


For input:



Enter values of X array:  1 2 3
Enter values of Y1 array:  1 2 1
Enter values of Y2 array:  1 4 1
Enter color:  red

Output :

For input:

Enter values of X array:  1 2 3 4 5
Enter values of Y1 array:  1 2 3 2 1
Enter values of Y2 array:  3 5 7 5 3
Enter color:  blue

Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.