matplotlib
grid lines
data visualization
python
graph elements

Matplotlib draw grid lines behind other graph elements

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Matplotlib, grid lines are just another set of artists in the draw order. If you want them behind your plotted lines, bars, or markers, the cleanest solution is usually to tell the axes to draw grid and ticks below the data using set_axisbelow(True). You can also control stacking more explicitly with zorder.

The Easiest Fix: set_axisbelow(True)

For most plots, this is the simplest and most reliable approach.

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3, 4]
4y = [3, 1, 4, 2]
5
6fig, ax = plt.subplots()
7ax.set_axisbelow(True)
8ax.grid(True)
9ax.plot(x, y, marker="o", linewidth=2)
10plt.show()

set_axisbelow(True) tells Matplotlib to render grid lines and ticks below the plot elements.

Using zorder Explicitly

If you want more control, assign zorder values yourself.

python
1import matplotlib.pyplot as plt
2
3x = [1, 2, 3, 4]
4y = [3, 1, 4, 2]
5
6fig, ax = plt.subplots()
7ax.grid(True, zorder=0)
8ax.plot(x, y, marker="o", linewidth=2, zorder=3)
9plt.show()

Lower zorder values are drawn first, so they appear behind higher ones.

This is useful when the plot contains many layered elements such as:

  • grids
  • filled areas
  • scatter points
  • annotations
  • error bars

Grid Behind Bars or Patches

Bar charts and patch-based plots often need explicit layering because patches can cover the grid completely.

python
1import matplotlib.pyplot as plt
2
3labels = ["A", "B", "C"]
4values = [5, 3, 7]
5
6fig, ax = plt.subplots()
7ax.set_axisbelow(True)
8ax.grid(axis="y")
9ax.bar(labels, values, zorder=2)
10plt.show()

Here the grid is under the bars because the axis is set below and the bars are drawn at a higher layer.

Why Grids Sometimes Still Look Wrong

The confusion usually comes from mixing two mechanisms:

  • the axis-wide set_axisbelow behavior
  • per-artist zorder

If a plot still does not look right, inspect both. For example, a filled region with a very low zorder can still visually dominate the grid if its alpha is opaque enough.

Minor Grids and Styling

Grid layering and grid styling are separate concerns. You can place the grid behind the data and still style it to be subtle.

python
ax.grid(True, which="major", linestyle="--", linewidth=0.6, alpha=0.5)
ax.minorticks_on()
ax.grid(True, which="minor", linestyle=":", linewidth=0.4, alpha=0.3)

This often improves readability more than raw layering changes alone.

For most everyday plotting, a good default is:

python
ax.set_axisbelow(True)
ax.grid(True, alpha=0.3)

Then only reach for explicit zorder adjustments when the plot contains unusual overlays or complex artist combinations.

Another Useful Setting: Axis-Specific Grids

Sometimes the visual problem is not overall layering but having too many grid lines. In those cases, drawing only the y-axis grid behind the data can make the chart much cleaner.

python
ax.set_axisbelow(True)
ax.grid(axis="y", alpha=0.25)

This is especially effective for bar charts and dashboards where horizontal reading guides are helpful but vertical grids add noise.

Common Pitfalls

A common mistake is changing the line zorder but forgetting the grid is still governed partly by the axis draw order.

Another mistake is assuming the grid is "in front" when the real issue is that the data artist has low transparency or a wide stroke that visually dominates it.

Developers also sometimes try to solve a styling problem with layering. A heavy black grid can still distract even when it is technically behind the plot.

Summary

  • Use ax.set_axisbelow(True) to place grid lines behind most plot elements.
  • Use zorder when you need fine-grained control over layering.
  • Grid order and grid styling are separate concerns.
  • Bars, patches, and filled regions often benefit from explicit layer choices.
  • Start simple with set_axisbelow(True) and only add more layering logic when needed.

Course illustration
Course illustration

All Rights Reserved.