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
Line Plot
Plot lines with markers, colors, and styles.
Bar Chart
Draw vertical, horizontal, and grouped bars.
Subplots
Arrange multiple axes with subplots and GridSpec.
Labels and Legend
Annotate plots with text, arrows, and legend options.
Styles and Themes
Apply built-in styles and customize rcParams.
Save Figure
Export figures to PNG, PDF, and SVG with DPI control.