October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan 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 Shade Between Two Curves in Matplotlib with fill_between

Use Matplotlib’s fill_between to shade between two curves, conditionally fill where one is higher, and handle crossings, steps, transparency, and vertical boundaries.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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

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:

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:

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.

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

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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

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.

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.

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
Windows Errors? Fix Them Before They SpreadFree repair scan

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.