fill_between() (Matplotlib)

ax.fill_between() shades the area between two curves (or between a curve and a horizontal baseline). It is used for confidence bands, highlighting regions, and emphasizing the area under a line.

Matplotlib

The basic call ax.fill_between(x, y) fills between the line y and y=0. Passing two y-arrays, fill_between(x, lower, upper), shades the band between them, which is the standard way to draw a confidence interval or a min/max envelope around a central line.

The where argument fills only where a condition holds — handy for highlighting the portions of a curve above a threshold, or shading recession periods on a time series. Set a low alpha so the shaded band sits behind the line rather than overpowering it.

fill_between returns a filled collection patch. For vertical bands across the whole height of a plot use axvspan, and for filling between vertical curves use fill_betweenx.

Syntax

ax.fill_between(x, y1, y2=0, where=None, alpha=None)

Examples

Python — Shades the area under the line.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
fb = ax.fill_between([1, 2, 3], [1, 2, 3], alpha=0.3)
print(fb is not None)

Output:

True

Learn it in a lesson

See also

Back to the Dictionary