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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use Matplotlib’s ax.fill_between(x, y1, y2) to shade the area between two curves. Pass both y-series explicitly: if you omit y2, Matplotlib fills between y1 and zero instead. For a region that should appear only where one curve is above the other, add a Boolean where mask.

Fill between two curves

The fill_between method creates one or more polygons between x coordinates and two y-coordinate series. It is available as an axes method, ax.fill_between, and through the pyplot wrapper. This example shades the space between two nonzero curves:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 200)
y1 = np.sin(x) + 2
y2 = 0.5 * np.cos(x) + 1

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.3)
ax.legend()
plt.show()

The third positional argument, y2, defines the second boundary. If you want to fill between a curve and the x-axis, omit it or set it to zero. The method returns a FillBetweenPolyCollection, so collection styling options such as color and alpha can be passed to the call. See the Matplotlib fill_between API.

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

Shade only where one curve is higher

Use where to select which portions of the x range are eligible for filling. For example, this shades only intervals where y1 is at least as large as y2:

ax.fill_between(x, y1, y2, where=(y1 >= y2), alpha=0.3)

The mask applies to intervals, not individual points: the segment between x[i] and x[i + 1] is filled only if the mask is true at both endpoints. Consequently, a single isolated True surrounded by False values does not fill a segment.

End the fill at a curve crossing

If the curves cross between sampled x values, interpolate=True calculates the intersection and extends the selected fill to that boundary:

ax.fill_between(x, y1, y2, where=(y1 >= y2), interpolate=True, alpha=0.3)

Without interpolation, the polygon uses the supplied x positions, which can clip a conditional fill at a crossing. Interpolation is most useful when the mask selects one side of a crossing and the visible boundary should meet the curves at their actual intersection. These parameters are documented in the API reference and illustrated in the official fill_between gallery example.

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

Choose the boundary shape for step data

For stepwise data, set step to match the convention used by the values. The setting controls how the boundary continues between x positions:

  • 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 successive x positions.
ax.fill_between(x, y1, y2, step="post", alpha=0.3)

For ordinary continuously varying data, leave step unset so the boundaries connect linearly between samples. Refer to the API documentation for the parameter details.

Fill horizontally between vertical curves

When y is the independent coordinate and the boundaries are vertical curves, use fill_betweenx(y, x1, x2) rather than fill_between. The argument order reflects the direction: provide y positions, then the two x boundaries.

ax.fill_betweenx(y, x1, x2, alpha=0.3)

The official fill_betweenx example notes that coarse sampling can leave small unfilled triangles near crossover points. If a boundary looks incomplete around an intersection, inspect the grid and increase sampling density where appropriate.

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

Style overlapping fills and save the figure

Transparency can keep overlapping filled regions distinguishable. Set alpha between 0 and 1 and choose a face color for each collection, for example ax.fill_between(x, y1, y2, color="tab:blue", alpha=0.25). Matplotlib’s gallery example demonstrates transparent fills and states that PostScript does not support alpha; its example names GIF, PNG, PDF, and SVG as formats that support alpha.

If transparency matters in the exported result, choose a format that supports it in the context documented by Matplotlib, such as PNG, PDF, or SVG, rather than PostScript. The behavior described here follows the stable API documentation identified as Matplotlib 3.11.2; the stable documentation URL can advance, so check the API for the version installed in your environment.

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.