DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober 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 Now×
Skip to content
World desk3 min

How to Use Matplotlib fill_between to Shade a Circle

Use the circle’s positive and negative square-root branches with Matplotlib fill_between to shade the full disk, not just the upper half.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use fill_between with the circle’s upper and lower semicircle equations. For a circle centered at (0, 0) with radius r, sample x from -r to r, calculate y = sqrt(r² - x²), then fill between y and -y. Set an equal aspect ratio so the result looks circular.

Shade the complete circle with two semicircle curves

A circle centered at the origin satisfies x² + y² = r². Solving for y gives two boundaries: the upper semicircle, sqrt(r² - x²), and the lower semicircle, -sqrt(r² - x²). Pass both arrays to fill_between:

import numpy as np
import matplotlib.pyplot as plt

r = 2.0
x = np.linspace(-r, r, 400)
y = np.sqrt(r**2 - x**2)

fig, ax = plt.subplots()
ax.fill_between(x, y, -y, color="cornflowerblue", alpha=0.5)
ax.plot(x, y, color="navy")
ax.plot(x, -y, color="navy")
ax.set_aspect("equal", adjustable="box")
plt.show()

The 400 x-values produce a smooth-looking sampled boundary for many ordinary plot sizes. The fill is a polygon constructed between sample points, not a mathematically continuous curve; increase the sample count if the edge looks coarse at your output size.

Why pass both boundaries?

fill_between(x, y1) uses zero as its second boundary by default. With only the positive semicircle, that shades the upper half between the arc and the x-axis. Passing -y explicitly fills the entire disk between its upper and lower edges. The function’s documented signature is fill_between(x, y1, y2=0, where=None, interpolate=False, step=None, *, data=None, **kwargs), and it returns a FillBetweenPolyCollection. See the Matplotlib fill_between API.

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

Why use an equal aspect ratio?

ax.set_aspect("equal", adjustable="box") makes one x-unit and one y-unit occupy the same display length. Without it, the filled mathematical disk can appear oval if the axes have different scales.

Choose valid x-values and a useful sample density

For the centered circle, use x-values only from -r through r. Outside that interval, r**2 - x**2 is negative, so the real-valued square root needed for the boundary is undefined. A low sample count can make the polygon’s outline visibly angular; a denser array makes the approximation smoother.

Shift the circle away from the origin

For a circle centered at (h, k) with radius r, its x-domain is h-r through h+r, and its two boundaries are k + sqrt(r**2 - (x-h)**2) and k - sqrt(r**2 - (x-h)**2). Use those arrays as the second and third arguments to fill_between:

x = np.linspace(h - r, h + r, 400)
y_upper = k + np.sqrt(r**2 - (x - h)**2)
y_lower = k - np.sqrt(r**2 - (x - h)**2)
ax.fill_between(x, y_upper, y_lower, alpha=0.5)

Use conditional filling only when the region calls for it

The basic circle needs no where mask: the entire interval between the two semicircle arrays is the desired region. A mask is useful when only selected intervals should be shaded. Matplotlib fills an interval only when the mask is true at both adjacent x-samples, so a lone True value does not fill a segment. The Matplotlib “Fill the area between two lines” example illustrates conditional filling.

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.

When a conditional region ends where two curves cross, set interpolate=True if the fill should reach the calculated intersection rather than stop at a nearby sampled x-node. This is useful for crossing curves; it is unnecessary for the ordinary full-circle fill.

Style the fill and preserve transparency when saving

Use color or facecolor to choose the fill color and alpha to control its opacity. Matplotlib’s basic fill_between example uses a half-transparent fill; linewidth=0 is another option when you want a band without a polygon edge. The basic fill_between example shows this style.

If the alpha transparency matters in an exported figure, save to PNG, PDF, or SVG. Matplotlib’s transparency example notes that PostScript does not support alpha.

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

When a polygon is a better representation

fill_between is convenient when a shape can be described as upper and lower y-values over x. If you already have the vertices of a closed outline—or the shape cannot be represented cleanly as two y-functions—use pyplot.fill, which fills a polygon from vertex coordinates. See the Matplotlib fill API. For a circle, the two-function method directly expresses the boundary and the region between its arcs.

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

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 *

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.

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
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.