Matplotlib Scatter Plot
A scatter plot uses plt.scatter(x, y) to draw each data point as an individual dot, making it the clearest way to spot relationships or clusters between two variables.
What a Scatter Plot Shows
A scatter plot places one marker for every (x, y) pair in your data instead of connecting the points with a line. That makes it well suited for questions like whether two variables tend to rise together, whether the data forms distinct groups, or whether a handful of points are unusual compared to the rest.
Basic Scatter Plot
Call plt.scatter(x, y) with two array-like arguments of the same length. Each pair (x[i], y[i]) becomes one point on the chart.
Example
import matplotlib.pyplot as plt
import numpy as np
study_hours = np.array([1, 2, 3, 4, 5, 6, 7, 8])
test_score = np.array([52, 58, 61, 68, 72, 80, 85, 90])
plt.scatter(study_hours, test_score)
plt.xlabel("Hours studied")
plt.ylabel("Test score")
plt.title("Study time vs. test score")
plt.show()Comparing Two Groups on One Chart
Calling plt.scatter() more than once before plt.show() draws each call's points on the same axes, using a different default color automatically. This is a simple way to compare two datasets, such as two classes or two experiment groups.
Example
import matplotlib.pyplot as plt
import numpy as np
class_a_hours = np.array([1, 2, 3, 4, 5])
class_a_scores = np.array([50, 55, 63, 70, 75])
class_b_hours = np.array([1, 2, 3, 4, 5])
class_b_scores = np.array([60, 68, 74, 82, 88])
plt.scatter(class_a_hours, class_a_scores, color="steelblue", label="Class A")
plt.scatter(class_b_hours, class_b_scores, color="darkorange", label="Class B")
plt.xlabel("Hours studied")
plt.ylabel("Test score")
plt.legend()
plt.show()Beyond a single color, plt.scatter() accepts arrays for c (color per point), s (size per point), and alpha (transparency). Passing an array to c lets Matplotlib color each point according to a value, such as a category or a measurement, and passing a colormap through cmap decides how those values map to colors.
Example
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(3)
x = np.random.rand(50) * 10
y = np.random.rand(50) * 10
temperature = np.random.rand(50) * 40
sizes = np.random.rand(50) * 300
plt.scatter(x, y, c=temperature, s=sizes, cmap="coolwarm", alpha=0.6)
plt.colorbar(label="Temperature (C)")
plt.title("Point color and size mapped to data")
plt.show()Exercise: Matplotlib Scatter
How does plt.scatter() differ from plt.plot() by default?