R
Rishtaara
Python: Zero to Professional
Lesson 21 of 40Article16 min

Matplotlib Labels, Grid, Subplot & Scatter

plt.title(), plt.xlabel(), plt.ylabel() describe the chart for readers.

Matplotlib Labels & Titles

plt.title(), plt.xlabel(), plt.ylabel() describe the chart for readers.

Always label axes in reports — unexplained charts confuse teammates.

Real-life example: Labels are signboards on a highway — without them, drivers guess the destination.

Title and axis labels
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [10, 20, 15])
plt.title("Weekly Scores")
plt.xlabel("Week")
plt.ylabel("Points")
plt.show()

Matplotlib Grid

plt.grid(True) adds background lines for easier reading of values.

Use alpha=0.3 for subtle grids that do not overpower the data.

Real-life example: Grid lines are notebook margins — they help you align numbers without doing math in your head.

Grid on
import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [5, 7, 4])
plt.grid(True, alpha=0.3)
plt.show()

Matplotlib Subplot

plt.subplot(rows, cols, index) or plt.subplots() show multiple charts in one figure.

Compare related views side by side — price vs volume, train vs test accuracy.

Real-life example: Subplots are a comic strip — four panels, one story, different angles.

Two subplots
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.plot([1, 2], [3, 4])
ax2.bar(["A", "B"], [5, 8])
plt.show()

Matplotlib Scatter

plt.scatter(x, y) shows individual points — ideal for correlations and clusters.

Add plt.scatter(x, y, c=colors, s=sizes) for color and size encoding.

Real-life example: Scatter plots are stars in the sky — each point is one observation, patterns form constellations.

Scatter plot
import matplotlib.pyplot as plt

hours = [1, 2, 3, 4, 5]
scores = [55, 60, 72, 78, 90]
plt.scatter(hours, scores)
plt.xlabel("Study hours")
plt.ylabel("Score")
plt.show()