๐จ Matplotlib Area & Fill Plots: Painting Data Like a Coloring Book
๐ The Big Picture: What Are Area Plots?
Imagine youโre coloring between lines in a coloring book. Thatโs exactly what area plots do with data! Instead of just drawing a line, you fill in the space beneath it with color.
Why does this matter?
- Lines show trends โ but filled areas show volume and magnitude
- Your eyes instantly see โhow muchโ instead of just โwhich directionโ
- Perfect for showing things that accumulate โ like money saved, rain fallen, or energy used
๐๏ธ 1. Area Plot โ The Basic Fill
Think of an Area Plot like coloring below a mountain outline. You draw the mountain top (your data line), then fill everything below with color.
What It Does
Fills the space between your data line and the x-axis (the bottom).
Simple Example
import matplotlib.pyplot as plt
days = [1, 2, 3, 4, 5]
visitors = [10, 25, 40, 30, 50]
plt.fill(days, visitors)
plt.title("Website Visitors")
plt.show()
Real-Life Use
- ๐ Show website traffic over time
- ๐ฐ Display savings growing in a bank account
- ๐ก๏ธ Temperature changes through the day
Pro Tip
Use alpha=0.5 to make the fill semi-transparent:
plt.fill(days, visitors, alpha=0.5)
๐ญ 2. Fill Between โ Color the Gap
Fill Between is like coloring the space between two lines. Imagine two mountain ridges โ you color only the valley between them!
What It Does
Fills the area between two curves (or between a curve and a value).
Simple Example
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.sin(x) + 0.5
plt.fill_between(x, y1, y2,
color='skyblue')
plt.title("Gap Between Waves")
plt.show()
Magic Trick: Conditional Coloring!
Color differently based on conditions:
plt.fill_between(x, y1, y2,
where=(y1 < y2),
color='green',
alpha=0.5)
plt.fill_between(x, y1, y2,
where=(y1 >= y2),
color='red',
alpha=0.5)
Real-Life Use
- ๐ Show profit vs loss (green above, red below)
- ๐ก๏ธ Comfortable temperature range
- ๐ Stock price confidence bands
๐ 3. Stacked Area Plot โ Layers Like a Cake
Remember stacking colored blocks? Stacked Area Plots show multiple data series piled on top of each other โ like layers of a rainbow cake!
What It Does
Shows how different parts contribute to a whole over time. Each layer stacks on top of the previous one.
Simple Example
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar', 'Apr']
games = [10, 15, 12, 18]
videos = [5, 8, 10, 12]
music = [8, 6, 9, 7]
plt.stackplot(months,
games, videos, music,
labels=['Games',
'Videos',
'Music'])
plt.legend(loc='upper left')
plt.title("Screen Time Breakdown")
plt.show()
Key Insight
- The total height = sum of all categories
- You see both the whole and the parts!
Real-Life Use
- ๐ต Budget breakdown over months
- โก Energy sources powering a city
- ๐ Population by age group over years
๐ช 4. Hatching Patterns โ Textures Instead of Colors
What if youโre printing in black and white? Or you need to help colorblind readers? Hatching adds patterns (lines, dots, crosses) instead of just colors!
What It Does
Adds texture patterns inside filled areas for accessibility and style.
Available Patterns
| Symbol | Pattern |
|---|---|
/ |
Diagonal lines |
\\ |
Opposite diagonal |
| ` | ` |
- |
Horizontal lines |
+ |
Plus signs |
x |
X patterns |
o |
Small circles |
O |
Large circles |
. |
Dots |
* |
Stars |
Simple Example
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 4, 3, 5]
plt.fill(x, y,
facecolor='lightblue',
hatch='///',
edgecolor='black')
plt.title("Hatched Area")
plt.show()
Combining with Fill Between
plt.fill_between(x, y1, y2,
facecolor='none',
hatch='xxx',
edgecolor='blue')
Real-Life Use
- ๐จ๏ธ Print-friendly graphs
- ๐๏ธ Accessibility for colorblind readers
- ๐ Scientific papers requiring patterns
๐บ๏ธ How It All Connects
graph TD A["Area Plots"] --> B["fill"] A --> C["fill_between"] A --> D["stackplot"] A --> E["hatch"] B --> F["Color below<br>one line"] C --> G["Color between<br>two lines"] D --> H["Stack multiple<br>series"] E --> I["Add patterns<br>for texture"]
๐ Quick Reference
| Function | Use Whenโฆ |
|---|---|
plt.fill() |
Single area, simple fill |
plt.fill_between() |
Gap between two curves |
plt.stackplot() |
Parts of a whole over time |
hatch='pattern' |
Need texture, not just color |
๐ก Remember This!
- Area plots = line charts with coloring below
- Fill between = color the gap between two lines
- Stacked = layers showing parts of a whole
- Hatching = patterns for accessibility
Youโre now ready to paint beautiful data stories! ๐จ
