Filling Areas (fill_between)

fill_between is a Matplotlib function that shades the area between a curve and a baseline — or between two curves — making it easy to highlight gaps, ranges, and confidence bands at a glance.

Learn Filling Areas (fill_between) in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll fill under a curve, shade the region between two lines, use the where argument to fill conditionally, build a confidence band, and flip the fill with fill_betweenx.

The simplest use of ax.fill_between() shades the area between a curve and the line y = 0 . Pass your x and y arrays and Matplotlib paints everything below the curve. Combine it with a regular ax.plot() on top and a low alpha so the outline stays crisp while the area is softly tinted.

What you'll see: a navy sine wave with the space between it and the x-axis softly tinted. Where the curve dips below zero in the second half, the shading appears below the axis — fill_between follows the curve in both directions.

Pass two y arrays and fill_between shades the gap between them — perfect for showing the spread between a high and a low series. Add the where= argument with a boolean condition to fill only part of the range. A classic example shades green where one curve beats another and red where it loses.

What you'll see: two wavy lines crossing back and forth. Where revenue rises above cost the gap fills green; where it dips below, the gap fills red. The alternating colored patches make profit and loss periods pop out instantly.

A confidence band is just a fill between y - err and y + err wrapped around a line — the same trick we used in the error bars lesson, now in its natural home. And when your independent variable runs up the vertical axis, reach for ax.fill_betweenx() , which shades between two x values across a range of y.

What you'll see: a crimson curve rising and leveling off, hugged by a soft red band that widens to the right. A faint gray vertical stripe between x = 4 and x = 6 (drawn with fill_betweenx) highlights that slice of the plot.

Replace each ___ to draw a line and shade a translucent band around it.

Draw the fill first or lower its alpha . A solid fill at alpha=1 will paint right over the line.

When using where= , add interpolate=True so the shaded regions meet exactly where the curves cross.

Your x , lower, and upper arrays must all match in length. Build the bounds from the same x .

Plot the average daily temperature with a shaded band between the daily low and high, and mark the freezing line.

Lesson complete — you can shade meaningful regions!

You filled under a curve, shaded between two lines, used where to fill conditionally, built a confidence band, and flipped the fill with fill_betweenx.

🚀 Up next: Stem, Step & Stair Plots — visualize discrete signals.

Practice quiz

By default, ax.fill_between(x, y) shades the area between the curve and what?

  • The top of the figure
  • The line y = 0
  • The mean of y
  • The first data point

Answer: The line y = 0. With a single y array, fill_between shades down to the horizontal line y = 0.

How do you shade the region between two curves?

  • ax.fill_between(x, y1, y2)
  • ax.fill(x, y1) twice
  • ax.plot(x, y1, y2)
  • ax.between(y1, y2)

Answer: ax.fill_between(x, y1, y2). Passing two y arrays, fill_between(x, y1, y2), shades the gap between them.

Which argument fills only where a condition is True?

  • mask=
  • only=
  • where=
  • cond=

Answer: where=. Pass a boolean array to where= and Matplotlib fills only the True x positions.

How do you make the fill semi-transparent?

  • transparent=True
  • alpha=0.3
  • opacity=0.3
  • fade=0.3

Answer: alpha=0.3. alpha takes a value between 0 and 1; lower numbers are more see-through.

What does fill_betweenx do differently from fill_between?

  • It fills between two x values across a range of y
  • It only fills triangles
  • It uses 3D
  • It ignores alpha

Answer: It fills between two x values across a range of y. fill_betweenx flips the roles, shading between two x values across a range of y.

Which argument removes gaps at the crossing points of two curves?

  • smooth=True
  • interpolate=True
  • snap=True
  • close=True

Answer: interpolate=True. interpolate=True makes the shaded regions meet exactly where the curves cross.

How do you fill between a curve and the line y = 5 (not 0)?

  • ax.fill_between(x, y, 5)
  • ax.fill_between(x, 5)
  • ax.fill_between(x, y, base=5)
  • ax.fill_between(x, y, offset=5)

Answer: ax.fill_between(x, y, 5). Pass 5 as the third positional argument to fill down to the line y = 5.

A confidence band around a line is drawn by filling between which bounds?

  • 0 and y
  • y and 2*y
  • y - err and y + err
  • min(y) and max(y)

Answer: y - err and y + err. A band is just ax.fill_between(x, y - err, y + err) wrapped around the line.

For where= to work correctly, the x, lower, and upper arrays must be...

  • Different lengths
  • The same length
  • Sorted descending
  • Integers only

Answer: The same length. All three arrays must match in length; build the bounds from the same x.

If a solid fill hides your line, a good fix is to...

  • Increase alpha to 1
  • Lower alpha or draw the fill first
  • Remove the line
  • Use fill_betweenx

Answer: Lower alpha or draw the fill first. Lower the fill's alpha (or draw it before the line) so the line stays visible.