Skip to content

Enhancement Request: grid below and ticks above #4243

Open
@u55

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.

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