5 min read min read
Scatter Plots
Learn to show relationships between two variables
Scatter Plots
When to Use Scatter Plots?
Scatter plots show relationships between two numbers:
- Height vs Weight
- Age vs Salary
- Study hours vs Test score
Each dot is one data point.
Basic Scatter Plot
code.py
import matplotlib.pyplot as plt
age = [25, 30, 35, 40, 45, 50]
salary = [40000, 50000, 60000, 65000, 70000, 80000]
fig, ax = plt.subplots()
ax.scatter(age, salary)
ax.set_xlabel('Age')
ax.set_ylabel('Salary')
ax.set_title('Age vs Salary')
plt.show()Change Marker Size
code.py
ax.scatter(age, salary, s=100) # s = sizeChange Color
code.py
ax.scatter(age, salary, color='red')Color by Value
code.py
# Each point has different color based on value
sizes = [10, 20, 30, 40, 50, 60]
fig, ax = plt.subplots()
scatter = ax.scatter(age, salary, c=sizes, cmap='viridis')
plt.colorbar(scatter) # Add color legend
plt.show()Size by Value
code.py
# Bigger points for higher values
experience = [2, 5, 8, 12, 15, 20]
ax.scatter(age, salary, s=[x*20 for x in experience])Multiple Groups
code.py
import matplotlib.pyplot as plt
# Group 1: Engineers
eng_age = [25, 30, 35, 40]
eng_salary = [60000, 70000, 80000, 90000]
# Group 2: Managers
mgr_age = [30, 35, 40, 45]
mgr_salary = [70000, 85000, 100000, 120000]
fig, ax = plt.subplots()
ax.scatter(eng_age, eng_salary, label='Engineers', color='blue')
ax.scatter(mgr_age, mgr_salary, label='Managers', color='red')
ax.set_xlabel('Age')
ax.set_ylabel('Salary')
ax.legend()
plt.show()Add Transparency
code.py
ax.scatter(x, y, alpha=0.5) # 0.5 = 50% transparentHelpful when points overlap.
Change Marker Shape
code.py
ax.scatter(x, y, marker='s') # squares
ax.scatter(x, y, marker='^') # triangles
ax.scatter(x, y, marker='x') # x marksComplete Example
code.py
import matplotlib.pyplot as plt
# Student data
hours_studied = [1, 2, 3, 4, 5, 6, 7, 8]
test_scores = [50, 55, 65, 70, 75, 85, 88, 95]
fig, ax = plt.subplots(figsize=(8, 6))
ax.scatter(hours_studied, test_scores,
s=100,
c=test_scores,
cmap='RdYlGn',
alpha=0.7)
ax.set_xlabel('Hours Studied')
ax.set_ylabel('Test Score')
ax.set_title('Study Time vs Test Score')
ax.grid(True, alpha=0.3)
plt.show()Key Points
- scatter() creates scatter plots
- s sets marker size
- c sets color (can be array)
- cmap sets color scheme
- alpha sets transparency
- marker sets shape
- Great for showing correlations
What's Next?
Learn bar charts for comparing categories.