Matplotlib grid() Lines and Styling Guide

Matplotlib grid lines make plots easier to read by giving the eye a reference for x and y values. In modern Matplotlib code, prefer the object-oriented API: create a figure and axes with plt.subplots(), then call ax.grid() on the axes you want to control.

The examples below were tested with Matplotlib 3.11.0 using the non-interactive Agg backend. They focus on common grid tasks: turning grids on, styling lines, showing only one axis, using minor grid lines, and keeping bars or lines above the grid.

Basic Matplotlib grid example

Call ax.grid(True) to show major grid lines on both axes. This is the simplest and clearest version for most line charts.

import matplotlib.pyplot as plt

x = [1, 2, 3]
y = [2, 4, 3]

fig, ax = plt.subplots()
ax.plot(x, y, marker="o")
ax.grid(True)

plt.show()

If you are using multiple subplots, call grid() on each axes object that needs grid lines. That keeps the behavior explicit and avoids accidental changes to the wrong plot.

Style grid color, width, and line style

grid() accepts line styling keyword arguments. Use light colors and modest line widths so the grid supports the data instead of overpowering it.

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [2, 4, 3], color="tab:blue")
ax.grid(True, color="0.85", linestyle="--", linewidth=1.0)

plt.show()

The color="0.85" value is a grayscale shortcut. You can also use named colors, hex colors, or RGB tuples.

Show grid lines on only one axis

Use the axis argument when you want grid lines only horizontally or vertically. For bar charts, a y-axis grid is often enough because it helps compare bar heights without cluttering the x-axis labels.

fig, ax = plt.subplots()
ax.bar(["A", "B"], [3, 5])
ax.grid(axis="y", linestyle="--", alpha=0.5)
ax.set_axisbelow(True)

plt.show()

set_axisbelow(True) draws grid lines behind plot elements. This is usually what you want for bars, markers, and thick lines.

Major and minor grid lines

Major grid lines follow the major ticks. Minor grid lines follow minor ticks, which you can enable with minorticks_on(). Use a lighter style for minor lines so the chart stays readable.

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [2, 4, 3])
ax.minorticks_on()
ax.grid(which="major", color="0.80", linewidth=1.0)
ax.grid(which="minor", color="0.90", linestyle=":", linewidth=0.6)

plt.show()

pyplot.grid() versus ax.grid()

matplotlib.pyplot.grid() still works, but it acts on the current axes. That can be convenient in quick notebooks, but it is less explicit in scripts with multiple subplots. ax.grid() makes the target axes obvious and is easier to maintain.

Common grid mistakes

  • Using grid lines that are darker than the plotted data.
  • Calling plt.grid() and accidentally changing the wrong subplot.
  • Turning on minor grid lines without enabling minor ticks.
  • Letting grid lines draw over bars or markers instead of behind them.
  • Using both x and y grids when one axis would be easier to read.

Grid lines should support the data

A grid is helpful when readers need to estimate values from the chart. It is less useful when the exact values are already labeled or when the plot has only a few points. If the grid competes with the line, markers, bars, or annotations, make it lighter or remove one axis of grid lines.

For dense charts, start with major grid lines only. Add minor grid lines only when they improve reading precision. A chart with both major and minor grid lines can quickly look noisy, especially on small screens.

Control ticks before adding a grid

Grid lines follow tick locations. If the grid looks too crowded or too sparse, adjust the ticks instead of trying to fix only the grid styling. Setting clear tick positions makes the grid predictable.

fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 4])
ax.set_xticks([0, 1, 2])
ax.set_yticks([0, 2, 4])
ax.grid(axis="both", color="0.85")
ax.set_axisbelow(True)

Saving a plot with grid lines

Grid lines are part of the figure, so they are included when you save the chart with savefig(). Use a suitable DPI and file format for the destination. PNG works well for blog posts and dashboards, while SVG or PDF can be better for vector output.

fig.savefig("plot-with-grid.png", dpi=150, bbox_inches="tight")

Troubleshooting Matplotlib grids

If the grid does not appear, confirm that you called grid() on the correct axes and that the grid color is not the same as the background. If minor grid lines do not appear, call minorticks_on() first. If the grid covers bars or markers, use set_axisbelow(True). If a subplot is missing a grid, remember that each axes object controls its own grid state.

Related Python guides

Official references

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted