October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Fill Between Horizontal and Vertical Lines in Matplotlib

Use fill_between for areas between horizontal curves and fill_betweenx for vertical curves. Examples cover constant lines, conditional masks, crossing interpolation and step fills.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.