Area and Fill Plots

Back

Loading concept...

๐ŸŽจ 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&lt;br&gt;one line"] C --> G["Color between&lt;br&gt;two lines"] D --> H["Stack multiple&lt;br&gt;series"] E --> I["Add patterns&lt;br&gt;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!

  1. Area plots = line charts with coloring below
  2. Fill between = color the gap between two lines
  3. Stacked = layers showing parts of a whole
  4. Hatching = patterns for accessibility

Youโ€™re now ready to paint beautiful data stories! ๐ŸŽจ

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.