Discrete Data Plots

Back

Loading concept...

📊 Discrete Data Plots: Step, Stem & Stairs

The Story of Data That Jumps (Not Flows!)

Imagine you’re watching a frog hop across lily pads. The frog doesn’t glide smoothly—it JUMPS from one pad to the next! 🐸

This is exactly how discrete data works. Unlike smooth, flowing data (like temperature throughout the day), discrete data has distinct, separate values—like counting marbles, tracking daily steps, or recording test scores.

Matplotlib gives us three special tools to show this “jumpy” nature:

  • Step Plot → Shows data like stair steps
  • Stem Plot → Shows data like lollipops on sticks
  • Stairs Plot → A cleaner version of step plots

Let’s explore each one!


🪜 Step Plot: The Staircase

What Is It?

A Step Plot draws your data as a staircase. Instead of connecting points with diagonal lines (like a regular line chart), it uses horizontal and vertical lines only.

Think of it like a robot walking:

  • 👟 Walk forward (horizontal)
  • 👟 Step up or down (vertical)
  • 👟 Repeat!

When Do We Use It?

Step plots are perfect for:

  • Digital signals (on/off switches)
  • Prices that change at specific times (stock market)
  • Counts that increase suddenly (website visitors per hour)

How to Create One

import matplotlib.pyplot as plt

# Data: hours and website visitors
hours = [0, 1, 2, 3, 4, 5]
visitors = [10, 10, 25, 25, 40, 40]

# Create step plot
plt.step(hours, visitors)
plt.xlabel('Hour')
plt.ylabel('Visitors')
plt.title('Website Visitors')
plt.show()

The where Parameter: Where Does the Step Happen?

This is the magic switch that controls when the step jumps:

Value Meaning Real-World Example
'pre' Step happens BEFORE the point “Price was this UNTIL now”
'post' Step happens AFTER the point “Price becomes this FROM now”
'mid' Step happens in the MIDDLE Centered around each point
# Three ways to step!
plt.step(x, y, where='pre')   # Step before
plt.step(x, y, where='post')  # Step after
plt.step(x, y, where='mid')   # Step in middle

Quick Example with where

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [1, 4, 2, 5]

fig, axes = plt.subplots(1, 3, figsize=(9, 3))

axes[0].step(x, y, where='pre')
axes[0].set_title("where='pre'")

axes[1].step(x, y, where='post')
axes[1].set_title("where='post'")

axes[2].step(x, y, where='mid')
axes[2].set_title("where='mid'")

plt.tight_layout()
plt.show()

🍭 Stem Plot: The Lollipop Chart

What Is It?

A Stem Plot looks like a garden of lollipops! Each data point has:

  • A vertical stick (stem) growing from a baseline
  • A circle (marker) at the top showing the value

It’s like planting flags in the ground—each flag marks where your data lives!

When Do We Use It?

Stem plots shine when you want to:

  • See individual values clearly (no overlapping)
  • Compare heights easily (like a bar chart, but cleaner)
  • Show sparse data (data with gaps)

Perfect for: signal processing, exam scores, discrete measurements.

How to Create One

import matplotlib.pyplot as plt

# Student scores
students = [1, 2, 3, 4, 5]
scores = [85, 92, 78, 95, 88]

# Create stem plot
plt.stem(students, scores)
plt.xlabel('Student')
plt.ylabel('Score')
plt.title('Test Scores')
plt.show()

Customizing Your Stems

You can make your lollipops look fancy!

plt.stem(x, y,
         linefmt='g-',    # Green stems
         markerfmt='ro',  # Red circle markers
         basefmt='b-')    # Blue baseline

Format codes:

  • linefmt → Style of the stem (line)
  • markerfmt → Style of the marker (top circle)
  • basefmt → Style of the baseline

Changing the Baseline

By default, stems grow from y=0. But you can change this!

# Stems grow from y=50
plt.stem(x, y, bottom=50)

This is useful when your data doesn’t start at zero—like temperatures that range from 60°F to 90°F.


🏗️ Stairs Plot: The Modern Step

What Is It?

plt.stairs() is like plt.step() but smarter and cleaner. It’s designed specifically for histogram-like data where you have:

  • Edges (boundaries between bins)
  • Values (height of each step)

Think of it like building blocks:

  • You define WHERE each block starts and ends (edges)
  • You define HOW TALL each block is (values)

Step vs Stairs: What’s the Difference?

Feature plt.step() plt.stairs()
Input x and y arrays (same length) values and edges (edges = values + 1)
Best for General discrete data Histogram bins
Edges Implicit Explicit

How to Create One

import matplotlib.pyplot as plt

# Bin edges: 0-10, 10-20, 20-30, 30-40
edges = [0, 10, 20, 30, 40]

# Values for each bin
values = [5, 12, 8, 3]

# Create stairs plot
plt.stairs(values, edges)
plt.xlabel('Range')
plt.ylabel('Count')
plt.title('Data Distribution')
plt.show()

Notice: 4 values need 5 edges (one more edge than values!)

Filling the Stairs

Want to color inside the stairs? Easy!

plt.stairs(values, edges, fill=True)

This creates a filled area chart—perfect for showing distributions.

Working with Histograms

plt.stairs() pairs beautifully with np.histogram():

import numpy as np
import matplotlib.pyplot as plt

# Random data
data = np.random.randn(1000)

# Get histogram values and edges
values, edges = np.histogram(data, bins=20)

# Plot with stairs
plt.stairs(values, edges, fill=True)
plt.title('Distribution of Random Data')
plt.show()

🎯 Choosing the Right Plot

Here’s your decision guide:

graph TD A[What's your data like?] --> B{Continuous bins?} B -->|Yes| C["Use STAIRS"] B -->|No| D{Show individual points?} D -->|Yes| E["Use STEM"] D -->|No| F["Use STEP"]

Quick Reference

Plot Best For Looks Like
Step Time-series with sudden changes Staircase
Stem Individual discrete values Lollipops
Stairs Histogram bins, distributions Building blocks

💡 Pro Tips

1. Combine with Other Plots

# Step plot with scatter points
plt.step(x, y, where='mid')
plt.scatter(x, y, color='red', zorder=5)

2. Add Grid for Readability

plt.step(x, y)
plt.grid(True, alpha=0.3)

3. Use Colors Wisely

  • Step plots: One solid color works best
  • Stem plots: Contrast between stem and marker
  • Stairs: Consider fill=True with transparency

🎨 Complete Example: All Three Together!

import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(1, 3, figsize=(10, 3))

# Data
x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 7, 4]

# Step Plot
axes[0].step(x, y, where='mid', color='blue')
axes[0].set_title('Step Plot')
axes[0].grid(True, alpha=0.3)

# Stem Plot
axes[1].stem(x, y, linefmt='g-', markerfmt='go')
axes[1].set_title('Stem Plot')
axes[1].grid(True, alpha=0.3)

# Stairs Plot
edges = [0.5, 1.5, 2.5, 3.5, 4.5, 5.5]
axes[2].stairs(y, edges, fill=True, alpha=0.7)
axes[2].set_title('Stairs Plot')
axes[2].grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

🚀 You’ve Got This!

Now you know the three musketeers of discrete data visualization:

  1. Step Plot (plt.step()) → For staircase patterns
  2. Stem Plot (plt.stem()) → For lollipop-style individual values
  3. Stairs Plot (plt.stairs()) → For clean histogram-like bins

Remember: When your data jumps instead of flows, reach for these plots!

Happy plotting! 📈

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.