Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 07

Grid Lines and Fonts

Add grid lines so readers can trace a value back to the axis, and style the title and axis names with a font dictionary.

Intermediate25 min
Grid Lines and Fonts hero Module 25 Lesson 7Grid Lines and Fonts hero Module 25 Lesson 7

Two small touches decide whether a chart can be read carefully or only glanced at. Grid lines let someone trace a point back to a number on the axis. Font settings set the hierarchy, so the title is read first and the axis names second.

A line chart with faint horizontal grid lines behind the dataA line chart with faint horizontal grid lines behind the data

Turning the grid on

plt.grid() draws guides for both axes. Pass axis="y" for horizontal guides only, which is the usual choice, because on most charts it is the height that has to be read precisely.

python
import matplotlib.pyplot as plt

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]

plt.plot(days, signups, marker="o")
plt.grid(axis="y")
plt.title("Guides on the value axis only")
plt.show()

print("axis choices:", ", ".join(["x", "y", "both"]))

Output:

text
axis choices: x, y, both

Styling the guides

Grid lines are lines, so they take the same arguments as any other: color, linewidth, linestyle, and alpha. Keep them faint. A grid that competes with the data has stopped helping.

python
import matplotlib.pyplot as plt

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]

plt.plot(days, signups, marker="o", color="#7C3AED")
plt.grid(axis="y", color="#94A3B8", linestyle=":", linewidth=1, alpha=0.7)
plt.title("A quiet grid")
plt.show()

print("the data should always be the darkest thing on the chart")

Output:

text
the data should always be the darkest thing on the chart

Fonts for the words

fontdict takes a dictionary of text settings and applies them to one piece of text. The useful keys are family, size, color, and weight. Build two dictionaries, one for the title and one for the axis names, and reuse them for a consistent look.

python
import matplotlib.pyplot as plt

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]

heading = {"family": "sans-serif", "size": 16, "color": "#1E293B", "weight": "bold"}
axis_text = {"family": "sans-serif", "size": 12, "color": "#475569"}

plt.plot(days, signups, marker="o")
plt.title("Signups last week", fontdict=heading)
plt.xlabel("day", fontdict=axis_text)
plt.ylabel("people", fontdict=axis_text)
plt.show()

print("title size:", heading["size"], "vs axis size:", axis_text["size"])

Output:

text
title size: 16 vs axis size: 12

Stick to the generic families sans-serif, serif, and monospace unless you know the exact font is installed wherever the chart will be drawn.

Padding and placement

loc moves a title left or right, and pad lifts it away from the plot. plt.tight_layout() is the fix for labels that get clipped at the edge of a saved image; it recomputes the spacing so everything fits.

python
import matplotlib.pyplot as plt

days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]

plt.plot(days, signups, marker="o")
plt.title("Signups last week", loc="left", pad=14)
plt.xlabel("day")
plt.ylabel("people")
plt.tight_layout()
plt.show()

print("tight_layout also rescues long tick labels")

Output:

text
tight_layout also rescues long tick labels

Key takeaways

  • plt.grid() adds guides; axis='y' keeps only the horizontal ones

  • Grid lines take color, linestyle, linewidth, and alpha

  • fontdict styles one piece of text with family, size, color, and weight

  • tight_layout stops labels being clipped at the edge

  • Next: Axis Limits and Ticks

  • Previous: Markers on a Line

Loading playground…

Check your understanding

Grid Lines and Fonts Quiz

What does plt.grid() with no arguments do?

1 / 10