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.
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.
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.
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_axisbelowbehavior - 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.
This often improves readability more than raw layering changes alone.
Recommended Pattern
For most everyday plotting, a good default is:
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.
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
zorderwhen 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.

