Skip to content
Matplotlib

Scatter Plot

Visualize relationships with size and color encodings.

By EZ4Code Team
scatterplot

Code

import matplotlib.pyplot as plt
import numpy as np

n = 50
x = np.random.rand(n)
y = 2 * x + np.random.randn(n) * 0.1
sizes = np.random.rand(n) * 200
colors = np.random.rand(n)

plt.scatter(x, y, s=sizes, c=colors, cmap="viridis",
            alpha=0.6, edgecolors="black")
plt.colorbar(label="intensity")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Scatter with size and color")
plt.show()

Explanation

scatter() maps each point's size and color to array values, encoding two extra dimensions in a 2D plot. cmap selects a colormap, alpha improves overlap readability, and colorbar adds a legend for the color scale. Edge styling keeps overlapping points distinguishable.

More Matplotlib Snippets