Repository navigation
Description
Hi matplotlib developers,
Currently, it is not possible to have the gridlines drawn below plotlines and simultaneously have ticks drawn above plotlines. With the set_axisbelow method, the gridlines and ticks are both set either above or below any plotlines. Here is a minimal working example:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams.update({
'font.size':16,
'axes.linewidth':6,
'xtick.major.width':4,
'ytick.major.width':4,
'xtick.major.size':13,
'ytick.major.size':13,
'grid.linewidth':4,
'grid.color':'0.8',
})
x = np.log(np.linspace(1e-3,1e+3,1e+3))
plt.figure()
ax1 = plt.subplot(211)
ax1.plot(x,lw=10)
ax1.set_xlim(-10,1000)
ax1.set_ylim(-8,6.8)
ax1.grid()
ax1.set_axisbelow(False)
ax2 = plt.subplot(212)
ax2.plot(x,lw=10)
ax2.set_xlim(-10,1000)
ax2.set_ylim(-8,6.8)
ax2.grid()
ax2.set_axisbelow(True)
plt.show()In the example above, in the top subplot, the set_axisbelow(False) method makes the gridlines above the plotline; while in the bottom subplot, the set_axisbelow(True) method makes the plotline above the ticks. This is unfortunate because, in my typical use case, I never want the gridlines above the data and I never want the ticks below the data, so I have to "simulate" a fake grid by drawing individual lines with axvline and axhline. This missing functionality was noticed at stackoverflow
http://stackoverflow.com/questions/19677963/matplotlib-keep-grid-lines-behind-the-graph-but-the-y-and-x-axis-above
a while ago, where it was called a "feature" by @tacaswell , so I am requesting a new functionality that would allow the gridlines to be set below all other artists without affecting the axis spines or ticks. Perhaps a new axes method named set_gridbelow would do the trick, or an optional kwarg to set_axisbelow.
In any case, I can't believe that I am the only person who wants this functionality to be built-in, so I hope that you consider adding this enhancement. Thanks.

Activity
petehuang commentedon Jan 9, 2017
#6287 does not separate ticks and grids, so that does not directly address this, but it does mention that doing so would require significant refactoring. Worth considering to evaluate impact-effort tradeoff for this
github-actions commentedon Mar 11, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
oscargus commentedon Apr 11, 2023
I'll reopen this as it seems like a sensible request.
lstngr commentedon Feb 28, 2026
Note also how grid lines may appear either behind (xaxis) or above (yaxis) the axis ticks!
results in
tacaswell commentedon Mar 1, 2026
@QuLogic @ksunden This is relevant to the ticker overhaul.
timhoffm commentedon Mar 1, 2026
The issue with grid lines overdrawing ticks it that the
Ticks are drawn monolithically inTick.draw. Therefore (draw grid, draw tick) happen immediately after another per direction. Note that therefore zorder of ticks and gridlines are ignored.What we would need is a way to first draw all grid lines and then draw all ticks. The general solution to this would be to make drawing always respect the artists zorder. This would require rearchitecting drawing so that
draw()does not immediately draw but instead adds the artists into a zorder-sorted list and only when the list is complete, draws are executed in that order.