The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Use fill_between(x, y1, y2) to shade between horizontal curves, and fill_betweenx(y, x1, x2) to shade between vertical curves. The key is which coordinate varies: x for fill_between, y for fill_betweenx.
Choose the function by the direction of the lines
Matplotlib’s fill_between fills the area between two horizontal curves; fill_betweenx fills between two vertical curves. In each case, one coordinate supplies the sequence of nodes and the other coordinate supplies the boundaries.
| Goal | Node sequence | Boundaries | Call |
|---|---|---|---|
| Fill between horizontal curves | x values | Two y values or curves | ax.fill_between(x, y1, y2) |
| Fill between vertical curves | y values | Two x values or curves | ax.fill_betweenx(y, x1, x2) |
The same operations are available through pyplot and an Axes object. The examples below use an Axes object, typically obtained with fig, ax = plt.subplots().
Fill between horizontal lines or curves
Pass x coordinates first, then the two y boundaries. Either boundary can be a scalar, which represents a constant horizontal line:
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
ax.fill_between(x, y1, y2) # between two y curves
ax.fill_between(x, y, 0) # between y(x) and the horizontal line y=0
For example, the following shades between a curve and the x-axis:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 200)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.fill_between(x, y, 0, facecolor="tab:blue", alpha=0.3)
plt.show()
In the pyplot API, the call returns a FillBetweenPolyCollection. Styling can be set with collection keyword arguments such as facecolor, alpha, and linewidth. See the Matplotlib pyplot.fill_between API.
Rank #2
Fill between vertical lines or curves
Pass y coordinates first, followed by the two x boundaries. A scalar boundary represents a constant vertical line:
ax.fill_betweenx(y, x1, x2) # between two x curves
ax.fill_betweenx(y, x, 0) # between x(y) and the vertical line x=0
To shade between two vertical boundaries, use fill_betweenx rather than swapping arguments to fill_between. The official example demonstrates shading between x=0 and a curve, then between that curve and x=1:
ax.fill_betweenx(y, 0, x1)
ax.fill_betweenx(y, x1, 1)
See the Matplotlib 3.11.2 Axes.fill_betweenx API and its vertical-fill example.
Limit the fill with a condition
Use where with a boolean mask aligned to the node sequence. For horizontal fills, the mask aligns with x; for vertical fills, it aligns with y. An interval is filled only when the mask is true at both of its neighboring nodes.
# Fill only where y1 is above y2
ax.fill_between(x, y1, y2, where=(y1 > y2))
# For vertical curves, the condition is evaluated along y
ax.fill_betweenx(y, x1, x2, where=(x1 > x2))
This neighboring-node rule matters: one isolated True surrounded by False values does not mark either adjacent interval for filling. If a mask changes at a point where the two curves cross, set interpolate=True so Matplotlib calculates the intersection and extends the fill to it instead of stopping at the sampled node:
ax.fill_between(x, y1, y2, where=(y1 > y2), interpolate=True)
The same option applies to fill_betweenx. The behavior of where and interpolation is documented in the fill-between API reference.
Best Value
Use step alignment for stepped data
For step-like curves, the step argument controls where each value extends relative to its coordinate. The same alignments apply to fill_betweenx, read along y instead of x.
step='pre': a value extends to the left of its x coordinate (or toward lower y for a vertical fill).step='post': a value extends to the right of its x coordinate (or toward higher y for a vertical fill).step='mid': the transition is placed halfway between neighboring coordinates.
Diagnose unexpected gaps and triangles
Check the mask and sample spacing as separate issues. A gap can follow directly from the rule that both nodes bounding an interval must pass the mask. At crossings, consider whether interpolate=True is needed. Separately, Matplotlib’s vertical-fill gallery example notes that coarse data gridding can leave unfilled triangles around crossover points; interpolating onto a finer grid is described there as a brute-force remedy. That note concerns sampling resolution and does not mean every visible gap has the same cause.
If a version-specific behavior is unexpected, compare it with the documentation for the installed Matplotlib release. The API details here reflect Matplotlib 3.11.2 documentation as of October 4, 2026.
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.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.




