Editor

Create a Color-Coded Scatter Plot

Advanced · Write the code · from Advanced Matplotlib: Subplots, Dual Axes, Styles, Annotations

From the lesson: Advanced Matplotlib: Subplots, Dual Axes, Styles, Annotations

You know how to create a single chart. But real-world reports rarely have just one. A data analyst might need four charts side by side, each zoomed into a different slice of the same dataset.

Read the lesson

Create a scatter plot where point color represents a third variable. Use numpy to generate 30 random x, y, and scores values (seed 42). Use ax.scatter() with c=scores, cmap='plasma', and s=80. Print the number of points and the colormap name from the scatter collection.

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
x = np.random.rand(30)
y = np.random.rand(30)
scores = np.random.randint(0, 100, 30)
# Create scatter plot with colormap
# Print number of points and colormap name

More Matplotlib Subplots exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz