๐จ Scatter Plots: Your Playground of Dots!
Imagine you have a big box of colorful stickers. You want to stick them on paper to show two things at once โ like how tall your friends are AND how many candies they have. Scatter plots are exactly that! Each sticker (dot) tells a story about two pieces of information.
๐ What is a Scatter Plot?
Think of a treasure map. Each โX marks the spotโ shows WHERE something is. A scatter plot is like that โ but instead of treasure, each dot shows two facts about something.
Real Life Example:
- ๐ Each dot = one person
- โก๏ธ Going right = theyโre taller
- โฌ๏ธ Going up = they have more candies
import matplotlib.pyplot as plt
# Heights and candies
heights = [4, 5, 5.5, 6]
candies = [10, 15, 20, 25]
plt.scatter(heights, candies)
plt.xlabel('Height (feet)')
plt.ylabel('Candies')
plt.title('My Friends')
plt.show()
The Magic: Now you can SEE if taller friends have more candies! ๐ฌ
๐ฏ Scatter Plot Basics
The Two Axes
Every scatter plot has two rulers:
- X-axis (horizontal) โ Goes left to right
- Y-axis (vertical) โ Goes up and down
Each dot sits at the โmeeting pointโ of two numbers!
graph TD A[Your Data] --> B[X value] A --> C[Y value] B --> D[Dot Position] C --> D D --> E[Scatter Plot!]
Simple Code Pattern
import matplotlib.pyplot as plt
# Your two lists of numbers
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 8, 7]
# Make the scatter plot
plt.scatter(x, y)
# Add labels (always do this!)
plt.xlabel('X Label')
plt.ylabel('Y Label')
plt.title('My Scatter Plot')
plt.show()
๐ Key Idea: plt.scatter(x, y) is all you need to start!
๐ Color by Value
What if each dot could have a different color based on another piece of information? Like coloring stickers based on how happy each friend is!
The Magic Parameter: c
The letter c stands for color. You give it a list of numbers, and matplotlib picks colors automatically!
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 8, 7]
happiness = [10, 30, 50, 70, 90]
plt.scatter(x, y, c=happiness)
plt.colorbar() # Shows color legend
plt.title('Colored by Happiness!')
plt.show()
Using Color Maps
Colormaps are like paint palettes. Some popular ones:
| Colormap | Best For |
|---|---|
viridis |
Default, good for all |
hot |
Heat/temperature |
cool |
Cold things |
rainbow |
Fun variety |
plt.scatter(x, y, c=happiness, cmap='hot')
plt.colorbar()
plt.show()
๐จ Pro Tip: Always add plt.colorbar() so people know what colors mean!
๐ Size by Value
Now for another superpower! What if BIGGER dots meant BIGGER values? Like making stickers larger for friends who ate MORE pizza! ๐
The Magic Parameter: s
The letter s stands for size. Give it numbers, and dots grow or shrink!
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 8, 7]
pizza_slices = [1, 3, 2, 5, 4]
# Multiply by 50 to make sizes visible
sizes = [p * 50 for p in pizza_slices]
plt.scatter(x, y, s=sizes)
plt.title('Bigger = More Pizza!')
plt.show()
Combining Color AND Size
The REAL magic happens when you use BOTH!
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 1, 8, 7]
happiness = [10, 30, 50, 70, 90]
pizza = [100, 300, 200, 500, 400]
plt.scatter(x, y,
c=happiness, # Color = happiness
s=pizza, # Size = pizza eaten
alpha=0.6) # Slightly see-through
plt.colorbar(label='Happiness')
plt.title('Color=Happy, Size=Pizza!')
plt.show()
graph TD A[Each Dot Shows] --> B[X Position] A --> C[Y Position] A --> D[Color = 3rd info] A --> E[Size = 4th info] B --> F[4 FACTS in 1 dot!] C --> F D --> F E --> F
๐ Amazing: One tiny dot can show FOUR pieces of information!
๐ Hexbin Plot
The Problem with Too Many Dots
Imagine 10,000 stickers all piled on top of each other. You canโt see anything! Itโs like a crowd of people โ you canโt count them all.
The Solution: Hexagons!
A hexbin plot divides your space into honeycomb cells. Instead of showing individual dots, it COLORS the hexagons based on how many dots are hiding inside!
graph TD A[1000s of Dots] --> B[Too Crowded!] B --> C[Group into Hexagons] C --> D[Color by Count] D --> E[Clear Pattern!]
Basic Hexbin
import matplotlib.pyplot as plt
import numpy as np
# Make lots of random data
np.random.seed(42)
x = np.random.randn(1000)
y = np.random.randn(1000)
# Hexbin instead of scatter
plt.hexbin(x, y, gridsize=20)
plt.colorbar(label='Count')
plt.title('Hexbin: Dots Grouped!')
plt.show()
Key Parameter: gridsize
- Small gridsize (10) = Fewer, bigger hexagons
- Large gridsize (50) = More, smaller hexagons
# Try different gridsizes
plt.hexbin(x, y, gridsize=15)
plt.colorbar()
plt.show()
When to Use What?
| Situation | Use This |
|---|---|
| Few dots (< 500) | scatter() |
| Many dots (500+) | hexbin() |
| Pattern in crowd | hexbin() |
| Show individual | scatter() |
๐ฎ Quick Reference
Scatter Plot Formula
plt.scatter(x, y,
c=colors, # Color by value
s=sizes, # Size by value
cmap='viridis',
alpha=0.7)
plt.colorbar()
Hexbin Formula
plt.hexbin(x, y,
gridsize=20,
cmap='viridis')
plt.colorbar()
๐ Summary: Your New Superpowers!
- Scatter Basics โ Each dot = two facts (x and y position)
- Color by Value โ Use
c=to add a third fact - Size by Value โ Use
s=to add a fourth fact - Hexbin โ Group crowded dots into colored hexagons
You did it! Now you can tell stories with dots! Each scatter plot is like a picture book where every dot whispers its secrets. ๐
Remember: The best scatter plots are like good stories โ they help you SEE patterns you couldnโt see before!