0
0
Matplotlibdata~5 mins

Area chart with plt.fill_between in Matplotlib

Choose your learning style9 modes available
Introduction

An area chart helps you see how values change over time or categories by filling the space under a line. It makes trends easy to spot.

To show how sales grow month by month with a shaded area under the sales line.
To visualize temperature changes during a day with the area under the temperature curve filled.
To compare two sets of data by filling areas between their lines.
To highlight the total amount of something over time, like website visitors per day.
Syntax
Matplotlib
plt.fill_between(x, y1, y2=0, where=None, interpolate=False, color=None, alpha=None, label=None)

x is the horizontal axis values (like time or categories).

y1 is the main vertical data to fill under.

Examples
Fill area between y and zero (default) along x.
Matplotlib
plt.fill_between(x, y)
Fill area between two lines y1 and y2.
Matplotlib
plt.fill_between(x, y1, y2)
Fill area with a light blue color and 50% transparency.
Matplotlib
plt.fill_between(x, y, color='skyblue', alpha=0.5)
Sample Program

This code draws a line for sales over 7 days and fills the area under the line with a light blue color. It helps you see how sales change each day.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

# Sample data: days and sales
x = np.arange(1, 8)  # Days 1 to 7
sales = np.array([100, 120, 90, 140, 130, 150, 170])

plt.plot(x, sales, color='blue', label='Sales')
plt.fill_between(x, sales, color='skyblue', alpha=0.5)

plt.title('Sales Over One Week')
plt.xlabel('Day')
plt.ylabel('Sales')
plt.legend()
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

Use alpha to control how see-through the filled area is.

You can fill between two lines by giving both y1 and y2.

Adding labels helps explain the chart when you add a legend.

Summary

Area charts fill space under a line to show trends clearly.

plt.fill_between is the function to create area charts in matplotlib.

Customize colors and transparency to make charts easy to read.