Data Science Scatter Plot
A scatter plot places two related variables on perpendicular axes so you can see at a glance whether they rise together, move oppositely, or show no relationship at all.
What a Scatter Plot Shows
Every point on a scatter plot is one observation, positioned by its x-value on the horizontal axis and its y-value on the vertical axis. With enough points, the overall shape of the cloud reveals whether the two variables are related.
Creating a Scatter Plot
matplotlib.pyplot.scatter takes two equal-length arrays - one for the x-coordinates, one for the y-coordinates - and draws one dot per pair.
Example
import numpy as np
import matplotlib.pyplot as plt
# hours of exercise per week vs resting heart rate (beats per minute)
exercise_hours = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8])
resting_hr = np.array([82, 80, 78, 74, 73, 70, 68, 65, 64])
plt.scatter(exercise_hours, resting_hr)
plt.xlabel("Exercise hours per week")
plt.ylabel("Resting heart rate (bpm)")
plt.show()The points here trend steadily downward: more weekly exercise lines up with a lower resting heart rate. That downward drift is what a negative correlation looks like on a scatter plot.
Reading Correlation From the Shape
- A trend rising left to right (positive correlation)
- A trend falling left to right (negative correlation)
- A cloud with no visible trend (little or no correlation)
- A bend or curve rather than a straight trend (a nonlinear relationship)
Example
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
x = np.random.rand(50) * 10
y = np.random.rand(50) * 10
plt.scatter(x, y)
plt.xlabel("x")
plt.ylabel("y")
plt.show()Once a scatter plot shows a roughly straight-line trend, you can go a step further and fit an actual line through the points - that's exactly what linear regression, covered next, does.
Exercise: Data Science Scatter Plot
What does a scatter plot primarily show?