Stack Plots & Area Charts

A stack plot layers several filled series on top of one another so the top edge traces the running total while each colored band shows how much one series contributes over time.

Learn Stack Plots & Area Charts in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

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 the area under a single curve, stack multiple series with ax.stackplot(), add a legend, normalize to a 100% chart, and change the baseline.

Before stacking many series, start with one. An area chart is just a line plot with the space beneath it filled. You can build it with ax.fill_between(x, y) or, for a single series, with ax.stackplot(x, y) . Both shade everything from the curve down to zero, giving a sense of volume rather than just a thin line.

What you'll see: a green filled region climbing from left to right, capped by a darker green line that traces the visitor count. The filled body makes the upward trend feel substantial — you see growing volume, not just a rising line.

Pass several y arrays to ax.stackplot() and Matplotlib layers them automatically: the first series sits on the bottom, the next stacks on top, and so on. The top edge of the whole stack shows the total . Add a labels list and call ax.legend() so each colored band is named.

What you'll see: three colored bands stacked into one growing mountain. Hydro forms a steady base, Wind adds a thick middle layer, and Solar — the fastest-growing — fattens the top over time. The very top edge shows the combined total climbing year after year.

Sometimes you care about share , not absolute size. A 100% stacked chart normalizes every column so the series always fill the full height — each band shows its percentage of the whole. Divide each series by the total at each x. You can also change the baseline argument to "sym" or "wiggle" for a centered streamgraph look.

What you'll see: a chart that fills the full 0–100 height at every month. The Mobile band steadily swells while Desktop shrinks, vividly showing the shift to mobile as a change in share rather than raw counts.

Replace each ___ to stack two series and show a legend.

Every stacked series must be the same length as x . Double-check that all arrays line up.

Pass a labels list to stackplot and call ax.legend() — you need both.

You divided by the wrong total. Sum the series element-wise with np.array values, then divide each one by that sum.

Show three product lines two ways: a normal stacked area chart on the left (absolute totals) and a 100% stacked chart on the right (share of the whole).

Lesson complete — you can show parts of a whole over time!

You filled a single area, stacked multiple series with ax.stackplot(), added a legend, normalized to a 100% chart, and learned how the baseline changes the look.

🚀 Up next: Checkpoint: Chart Types — put every chart type you've learned to work.

Practice quiz

Which method stacks several filled series as areas?

  • ax.areaplot()
  • ax.stackplot()
  • ax.stack()
  • ax.fillplot()

Answer: ax.stackplot(). ax.stackplot(x, a, b, c) layers the series into a stacked area chart.

What does the top edge of a stack plot show?

  • The first series only
  • The smallest series
  • The running total
  • The average

Answer: The running total. The top edge traces the running total of all stacked series.

How do you give each stacked band a name?

Pass a labels list to stackplot, then call ax.legend().

What two steps make a legend appear on a stack plot?

  • Only call ax.legend()
  • Only pass labels=
  • Pass labels= and call ax.legend()
  • Set baseline='legend'

Answer: Pass labels= and call ax.legend(). You need both a labels list and a call to ax.legend().

How do you make a 100% stacked chart?

  • Pass percent=True
  • Divide each series by the total at each x
  • Use baseline='100'
  • Call ax.normalize()

Answer: Divide each series by the total at each x. Normalize each series by the element-wise total so they fill the full height.

Which baseline value creates a streamgraph look?

  • 'zero'
  • 'edge'
  • 'flat'
  • 'wiggle'

Answer: 'wiggle'. baseline='wiggle' (or 'sym') centers the stack for a flowing streamgraph.

What is the default baseline for stackplot?

  • 'zero'
  • 'sym'
  • 'wiggle'
  • 'weighted_wiggle'

Answer: 'zero'. The default 'zero' stacks upward from the x-axis.

Which call fills the area under a single curve?

  • ax.under(x, y)
  • ax.shade(x, y)
  • ax.area(x, y)
  • ax.fill_between(x, y)

Answer: ax.fill_between(x, y). ax.fill_between(x, y) shades from the curve down to zero.

What error appears if a stacked series differs in length from x?

  • KeyError
  • ValueError: shape mismatch
  • NameError
  • ImportError

Answer: ValueError: shape mismatch. Every stacked series must match the length of x, or a shape mismatch ValueError occurs.

On a 100% stacked chart, what should ax.set_ylim be?

  • (0, 1)
  • (0, 10)
  • (0, 100)
  • (0, 360)

Answer: (0, 100). After normalizing to percentages, set the y-axis to (0, 100).