Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
Title: 'fill_between()'
Description: 'Fills the area between two curves or between a curve and a baseline in a plot.'
Subjects:
- 'Data Science'
- 'Data Visualization'
Tags:
- 'Graphs'
- 'Libraries'
- 'Matplotlib'
- 'Methods'
CatalogContent:
- 'learn-python-3'
- 'paths/data-science'
---

The **`fill_between()`** function in Matplotlib's `pyplot` module creates a shaded region between two curves or between a curve and a baseline. It is commonly used to visualize ranges, confidence intervals, thresholds, and differences between datasets.

## Syntax

```pseudo
matplotlib.pyplot.fill_between(x, y1, y2=0, where=None, **kwargs)
```

**Parameters:**

- `x`: Sequence of x-coordinates.
- `y1`: First set of y-values.
- `y2` (optional): Second set of y-values. Defaults to `0`, creating a fill between the curve and the x-axis.
- `where` (optional): A boolean mask that controls where to fill.
- `**kwargs`: Additional styling options such as `color`, `alpha`, or `label`.

**Return value:**

Returns a `PolyCollection` object that represents the shaded region.

## Example

The example below shows how to fill the area between a curve and the x-axis:

```py
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 200)
y = np.sin(x)

plt.plot(x, y)
plt.fill_between(x, y, 0, alpha=0.3)

plt.title("Filled Area Under Sine Curve")
plt.show()
```

The output of this code is:

![A sine wave plot with a shaded region under the curve created using fill_between(), showing area fill and transparency.](https://raw.githubusercontent.com/Codecademy/docs/main/media/Figure_1.png)

## Example

This example fills the area between two curves:

```py
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 5, 100)
y1 = x
y2 = x**2

plt.plot(x, y1, label="y = x")
plt.plot(x, y2, label="y = x^2")

plt.fill_between(x, y1, y2, alpha=0.3)

plt.legend()
plt.show()
```

![A plot of y = x and y = x² with the area between the two curves shaded using fill_between(), illustrating curve comparison and filled regions.](https://raw.githubusercontent.com/Codecademy/docs/main/media/Figure_2.png)
Binary file added media/Figure_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/Figure_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading