In this tutorial, we’ll try to understand how to fill in the area between lines in a matplotlib plot with the help of examples.

We can easily fill in the area between lines in a Matplotlib plot by using the following functions available in the `matplotlib.pyplot`

module:

`fill_between()`

– to fill the area between horizontal curves`fill_betweenx()`

– to fill the area between vertical curves

## Using the `matplotlib.pyplot.fill_between()`

method

It is a method used to fill the area between horizontal curves on a matplotlib plot.

**Basic Syntax:**

matplotlib.pyplot.fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, *, data=None, **kwargs)

**Parameters:**

**x**-array (length N): The x coordinates of the nodes defining the curves.**y1**-array (length N) or scalar: The y coordinates of the nodes defining the first curve.**y2**-array (length N) or scalar: The y coordinates of the nodes defining the second curve.**where**-array of bool (length N), optional: Define where to exclude some horizontal regions from being filled.**interpolate**-bool, default: False: This option is only relevant if the`where`

parameter is used and the two curves are crossing each other.

For more details, refer this.

## Examples

Now, we’ll try to understand the above methods, with some worked-out examples.

### Example 1 – Filling area between two lines

import matplotlib.pyplot as plt import numpy as np #define x and y x = np.arange(10,20) y = np.arange(10,20) #create plot of values with specified ylim plt.ylim(0,20) plt.plot(x,y) #fill in area between the lines plt.fill_between(x, y,5, color='red')

Output:

In the above example, we –

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

- First, import the required modules.
- Then, we define the x and y values for the plot.
- Then, we adjust the y limits of the plot using
`plt.ylim`

(Refer this). - Then, we plot the x and y values.
- Then, we filled the area between the given x and y values. As the y2 value is 5 (a scaler value), the graph is filled from y = 5 with red color.

### Example 2 – Filling area under a curve

import matplotlib.pyplot as plt import numpy as np #define x and y x = np.arange(0,100,1) y = x**3 #create plot plt.plot(x,y) #fill in area between the curve and line plt.fill_between(x, y, color='green')

Output:

In the above example, we –

- First, import the required modules.
- Then, we define the x and y values (a cubic equation
`y = x^3`

) for the plot. - Then, we plot the graph.
- Then, we fill the region below the graph (using the
`fill_between()`

function) until the x axis because the y2 is by default 0.

### Example 3 – Filling area above the curve

import matplotlib.pyplot as plt import numpy as np #define x and y x = np.arange(0,10) y = x**3 #create plot plt.plot(x,y) #fill in area above the curve plt.fill_between(x, y, np.max(y), alpha=.3)

Output:

In the above example, we –

- First, import the required modules.
- Then, we define the x and y values (a cubic equation
`y = x^3`

) for the plot. - Then, we plot the graph.
- Then, we fill the region above the graph (using the
`fill_between()`

function) by setting the y2 as the maximum value of all the y values.

### Example 4 – Filling area only under specified condition

import matplotlib.pyplot as plt import numpy as np #define x and y x = np.arange(-10,10) y = x**3 #create plot plt.plot(x,y) #fill in area above the curve plt.fill_between(x, y,color='blue', alpha=.3, where = (y>0)) plt.fill_between(x, y,color='green', alpha=.3, where=(y<=0))

Output:

In the above example, we –

- First, import the required modules.
- Then, we define the x and y values (a cubic equation
`y = x^3`

) for the plot. - Then, we plot the graph.
- Then, we fill the area which is above the x axis with blue color and the area below the x axis with green using the
`where`

parameter of the`fill_between()`

function.

## Using the `matplotlib.pyplot.fill_betweenx()`

method

It is a method used to fill the area between vertical cuves on a matplotlib plot.

**Basic Syntax:**

matplotlib.pyplot.fill_betweenx(y, x1, x2=0, where=None, step=None, interpolate=False, *, data=None, **kwargs)

**Parameters:**

**y**-array (length N): The y coordinates of the nodes defining the curves.**x1**-array (length N) or scalar: The x coordinates of the nodes defining the first curve.**x2**-array (length N) or scalar: The x coordinates of the nodes defining the second curve.**where**-array of bool (length N), optional: Define where to exclude some horizontal regions from being filled.**interpolate**-bool, default: False: This option is only relevant if where is used and the two curves are crossing each other.

For more details, refer this.

## Examples

Now, we’ll try to understand the above method, with some worked-out examples.

### Example 1 – Filling area between vertical lines

import matplotlib.pyplot as plt import numpy as np #define x and y x = np.arange(10,20) y = np.arange(10,20) #create plot of values plt.plot(x,y) #fill in area between the lines plt.fill_betweenx(y, 12,15, color='red')

Output:

In the above example, we –

- First, import the required modules.
- Then, we define the x and y values for the plot.
- Then, we plot the graph.
- Then, we fill the area between x1=12 and x2 = 15 with all the y values specified in the plot using
`fill_betweenx()`

function.

### Example 2 – Filling area above the graph

import matplotlib.pyplot as plt import numpy as np #define x and y y = np.arange(0,100,1) x = y**3 #create plot plt.plot(x,y) #fill in area between the curve and line plt.fill_betweenx(y, x, color='green')

Output:

In the above example, we –

- First, import the required modules.
- Then, we define the x and y values (a cubic equation `x = y^3`) for the plot.
- Then, we plot the graph.
- Then, we fill the area above the curve in the plot using
`fill_betweenx()`

function.

You might also be interested in –

- How to Draw a Rectangle in a Matplotlib Plot?
- How to remove the legend border (frame) in Matplotlib?
- How to change the legend position in Matplotlib?

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**