# Fill Area Between Lines in Matplotlib

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:

1. `fill_between()` – to fill the area between horizontal curves
2. `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 –

1. First, import the required modules.
2. Then, we define the x and y values for the plot.
3. Then, we adjust the y limits of the plot using `plt.ylim` (Refer this).
4. Then, we plot the x and y values.
5. 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 –

1. First, import the required modules.
2. Then, we define the x and y values (a cubic equation `y = x^3`) for the plot.
3. Then, we plot the graph.
4. 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 –

1. First, import the required modules.
2. Then, we define the x and y values (a cubic equation `y = x^3`) for the plot.
3. Then, we plot the graph.
4. 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 –

1. First, import the required modules.
2. Then, we define the x and y values (a cubic equation `y = x^3`) for the plot.
3. Then, we plot the graph.
4. 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 –

1. First, import the required modules.
2. Then, we define the x and y values for the plot.
3. Then, we plot the graph.
4. 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 –

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

You might also be interested in –

• 