Use Matplotlib’s ax.fill_between(x, y1, y2) to shade the area between two curves. Pass both y-values explicitly: if you omit y2, Matplotlib fills between y1 and zero instead. For conditional shading—such as filling only where one curve is higher—add a boolean where mask.
Fill between two curves
fill_between creates one or more filled polygons between x-coordinates and two y-coordinate series (or scalar y-values). The pyplot function is a wrapper for the Axes method. Using an axes object directly is convenient when building a figure:
As an Amazon Associate I earn from qualifying purchases.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x, y1, label="Curve 1")
ax.plot(x, y2, label="Curve 2")
ax.fill_between(x, y1, y2, alpha=0.25, label="Between curves")
ax.legend()
plt.show()
The call above shades between the curves at each supplied x-coordinate. The API returns a FillBetweenPolyCollection, which can be styled using collection properties such as face color and transparency. See the Matplotlib fill_between API reference.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Shade only where one curve is above the other
Pass a boolean condition as where. For example, y1 > y2 selects intervals where the first curve is higher:
#1 Best Overall
ax.fill_between(x, y1, y2, where=(y1 > y2), alpha=0.3)
A segment between x[i] and x[i + 1] is filled only if the mask is true at both ends. An isolated True surrounded by False values therefore fills no segment. This interval-based behavior matters when a condition is true at a single sampled point but not at either neighboring point.
Handle curves that cross
When a selected interval contains a crossing, use interpolate=True if the fill boundary should end at the curves’ intersection between sampled x-values:
Rank #2
ax.fill_between(
x, y1, y2,
where=(y1 > y2),
interpolate=True,
alpha=0.3
)
With the default interpolate=False, polygon nodes are limited to the supplied x-positions, so the filled region may be clipped at the crossing rather than reaching the intersection. The official fill-between gallery example demonstrates conditional fills and transparency.
Recommended Free Tools
Match the boundary to stepwise data
For values that change in steps rather than along a continuously interpolated line, choose the step setting that matches how the data should be drawn:
step='pre': each y-value continues to the left of its x-position.step='post': each y-value continues to the right of its x-position.step='mid': transitions occur halfway between adjacent x-positions.
For example, ax.fill_between(x, y1, y2, step='post') uses post-step boundaries. These settings affect the shape of the filled boundary; use the convention that matches the plotted data.
Choose transparency and output format
Use alpha to make overlapping fills easier to see, for example alpha=0.25. Matplotlib’s gallery example notes that PostScript does not support alpha transparency; it identifies GIF, PNG, PDF, and SVG as formats that support alpha in that example’s context. If transparency matters, choose a supported output format rather than relying on a PostScript export.
Use fill_betweenx for vertical curves
When y is the independent coordinate and the boundaries are vertical curves, use fill_betweenx(y, x1, x2) instead of fill_between:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
ax.fill_betweenx(y, x1, x2, alpha=0.25)
The two functions differ in which coordinate supplies the independent direction: fill_between fills between y-values across x, while fill_betweenx fills between x-values across y. The official fill_betweenx example also shows that coarse sampling can leave unfilled triangular gaps around crossover points. If the boundary looks incomplete near a crossing, inspect whether the sampled grid is fine enough to represent it.
Best Value
Check the installed Matplotlib version
The stable API reference used here identifies itself as Matplotlib 3.11.2, but the stable documentation URL can advance. For version-specific behavior, compare the reference with the Matplotlib version installed in your environment. The relevant documentation includes the fill_between API, the collections API, and the gallery example.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




