Fill and area

fill_between, stacked area, and a confidence band.

fill_between shades the region between two curves (or a curve and zero). Use it for a band, a stacked area, or “above a threshold”.

Goal

Fill under a curve, draw a band, and stack two series.

Under a curve

x = np.linspace(0, 2 * np.pi, 80)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y, color="#1d4f7a")
ax.fill_between(x, y, 0, alpha=0.25, color="#1d4f7a")
ax.set_title("fill_between y and 0")
plt.show()

A band

x = np.linspace(0, 4, 80)
mean = np.exp(-0.3 * x) * np.sin(3 * x) + 1
fig, ax = plt.subplots()
ax.plot(x, mean, color="#1d4f7a")
ax.fill_between(x, mean - 0.2, mean + 0.2, alpha=0.25, color="#1d4f7a")
ax.set_title("mean ± 0.2")
plt.show()

Stacked area

months = np.arange(4)
nairobi = np.array([50, 40, 80, 150], dtype=float)
kisumu = np.array([70, 80, 120, 180], dtype=float)
fig, ax = plt.subplots()
ax.fill_between(months, 0, nairobi, label="Nairobi", alpha=0.7)
ax.fill_between(months, nairobi, nairobi + kisumu, label="Kisumu", alpha=0.7)
ax.set_xticks(months, ["Jan", "Feb", "Mar", "Apr"])
ax.legend()
ax.set_ylabel("mm")
ax.set_title("Stacked (not overlaid)")
plt.show()

The second fill sits on top of Nairobi’s values. That is a stack, not two independent fills from zero.

You should see

Alpha under 1 lets gridlines show through. Too much fill hides the line — keep the plot on top.