Skip to content

Enhancement Request: grid below and ticks above #4243

Open

Description

@u55

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()

grid_below

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

  1. added this to the milestone on Mar 18, 2015
  2. petehuang commented on Jan 9, 2017

    @petehuang
    Contributor

    #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

  3. modified the milestones: , on Jan 10, 2017
  4. github-actions commented on Mar 11, 2023

    @github-actions

    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!

  5. oscargus commented on Apr 11, 2023

    @oscargus
    Member

    I'll reopen this as it seems like a sensible request.

  6. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Apr 11, 2023
  7. lstngr commented on Feb 28, 2026

    @lstngr

    while in the bottom subplot, the set_axisbelow(True) method makes the plotline above the ticks.

    Note also how grid lines may appear either behind (xaxis) or above (yaxis) the axis ticks!

    import matplotlib.pyplot as plt
    plt.rcParams["xtick.top"] = True
    plt.rcParams["xtick.direction"] = "in"
    plt.rcParams["ytick.right"] = True
    plt.rcParams["ytick.direction"] = "in"
    plt.rcParams["xtick.major.size"] = 3.5 * 2
    plt.rcParams["xtick.minor.size"] = 2.0 * 2
    plt.rcParams["ytick.major.size"] = 3.5 * 2
    plt.rcParams["ytick.minor.size"] = 2.0 * 2
    
    plt.subplots(figsize=(1.6, 1.2), dpi=300)
    plt.loglog([], [])
    plt.xlim(1e0, 1e3)
    plt.ylim(1e0, 1e3)
    plt.grid(which="both", lw=0.5)
    plt.show()

    results in

    Image
  8. tacaswell commented on Mar 1, 2026

    @tacaswell
    Member

    @QuLogic @ksunden This is relevant to the ticker overhaul.

  9. timhoffm commented on Mar 1, 2026

    @timhoffm
    Member

    The issue with grid lines overdrawing ticks it that the Ticks are drawn monolithically in Tick.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    New featurekeepItems to be ignored by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions