Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 515ba4b

Browse files
committed
Merge pull request #6287 from efiring/axisbelow-lines
ENH: add axisbelow option 'line', make it the default
2 parents 20c2961 + efffe26 commit 515ba4b

File tree

10 files changed

+59
-9
lines changed

10 files changed

+59
-9
lines changed

doc/users/whats_new/style_changes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ Plot layout
7272
- Ticks now point outward by default. To have ticks pointing inward,
7373
use the ``rcParams`` ``xtick.direction`` and ``ytick.direction``.
7474

75+
- Ticks and grids are now plotted above solid elements such as
76+
filled contours, but below lines. To return to the previous
77+
behavior of plotting ticks and grids above lines, set
78+
``rcParams['axes.axisbelow'] = False``.
79+
7580
- By default, caps on the ends of errorbars are not present. Use the
7681
rcParam ``errorbar.capsize`` to control this.
7782

examples/api/custom_scale_example.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
from matplotlib import scale as mscale
66
from matplotlib import transforms as mtransforms
77
from matplotlib.ticker import Formatter, FixedLocator
8+
from matplotlib import rcParams
9+
10+
11+
# BUG: this example fails with any other setting of axisbelow
12+
rcParams['axes.axisbelow'] = False
813

914

1015
class MercatorLatitudeScale(mscale.ScaleBase):

lib/matplotlib/axes/_base.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from matplotlib.artist import allow_rasterization
3737

3838
from matplotlib.rcsetup import cycler
39+
from matplotlib.rcsetup import validate_axisbelow
3940

4041
rcParams = matplotlib.rcParams
4142

@@ -459,8 +460,9 @@ def __init__(self, fig, rect,
459460
*aspect* [ 'auto' | 'equal' | aspect_ratio ]
460461
*autoscale_on* [ *True* | *False* ] whether or not to
461462
autoscale the *viewlim*
462-
*axisbelow* draw the grids and ticks below the other
463-
artists
463+
*axisbelow* [ *True* | *False* | 'line'] draw the grids
464+
and ticks below or above most other artists,
465+
or below lines but above patches
464466
*cursor_props* a (*float*, *color*) tuple
465467
*figure* a :class:`~matplotlib.figure.Figure`
466468
instance
@@ -2326,12 +2328,16 @@ def draw(self, renderer=None, inframe=False):
23262328
artists.remove(spine)
23272329

23282330
if self.axison and not inframe:
2329-
if self._axisbelow:
2331+
if self._axisbelow is True:
23302332
self.xaxis.set_zorder(0.5)
23312333
self.yaxis.set_zorder(0.5)
2332-
else:
2334+
elif self._axisbelow is False:
23332335
self.xaxis.set_zorder(2.5)
23342336
self.yaxis.set_zorder(2.5)
2337+
else:
2338+
# 'line': above patches, below lines
2339+
self.xaxis.set_zorder(1.5)
2340+
self.yaxis.set_zorder(1.5)
23352341
else:
23362342
for _axis in self._get_axis_list():
23372343
artists.remove(_axis)
@@ -2431,9 +2437,9 @@ def set_axisbelow(self, b):
24312437
Set whether the axis ticks and gridlines are above or below most
24322438
artists
24332439
2434-
ACCEPTS: [ *True* | *False* ]
2440+
ACCEPTS: [ *True* | *False* | 'line' ]
24352441
"""
2436-
self._axisbelow = b
2442+
self._axisbelow = validate_axisbelow(b)
24372443
self.stale = True
24382444

24392445
@docstring.dedent_interpd

lib/matplotlib/rcsetup.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ def validate_string_or_None(s):
175175
except ValueError:
176176
raise ValueError('Could not convert "%s" to string' % s)
177177

178+
def validate_axisbelow(s):
179+
try:
180+
return validate_bool(s)
181+
except ValueError:
182+
if isinstance(s, six.string_types):
183+
s = s.lower()
184+
if s.startswith('line'):
185+
return 'line'
186+
raise ValueError('%s cannot be interpreted as'
187+
' True, False, or "line"' % s)
188+
178189

179190
def validate_dpi(s):
180191
"""confirm s is string 'figure' or convert s to float or raise"""
@@ -1002,7 +1013,7 @@ def validate_animation_writer_path(p):
10021013
'errorbar.capsize': [0, validate_float],
10031014

10041015
# axes props
1005-
'axes.axisbelow': [False, validate_bool],
1016+
'axes.axisbelow': ['line', validate_axisbelow],
10061017
'axes.hold': [True, validate_bool],
10071018
'axes.facecolor': ['w', validate_color], # background color; white
10081019
'axes.edgecolor': ['k', validate_color], # edge color; black

lib/matplotlib/tests/test_axes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from matplotlib.testing.noseclasses import KnownFailureTest
2626
import matplotlib.pyplot as plt
2727
import matplotlib.markers as mmarkers
28+
import matplotlib.patches as mpatches
2829
from numpy.testing import assert_allclose, assert_array_equal
2930
import warnings
3031
from matplotlib.cbook import IgnoredKeywordWarning
@@ -4417,6 +4418,27 @@ def test_date_timezone_x_and_y():
44174418
plt.plot_date(time_index, time_index, tz='US/Eastern', ydate=True)
44184419

44194420

4421+
@image_comparison(baseline_images=['axisbelow'],
4422+
extensions=['png'], remove_text=True)
4423+
def test_axisbelow():
4424+
# Test 'line' setting added in 6287.
4425+
# Show only grids, not frame or ticks, to make this test
4426+
# independent of future change to drawing order of those elements.
4427+
fig, axs = plt.subplots(ncols=3, sharex=True, sharey=True)
4428+
settings = (False, 'line', True)
4429+
4430+
for ax, setting in zip(axs, settings):
4431+
ax.plot((0, 10), (0, 10), lw=10, color='m')
4432+
circ = mpatches.Circle((3, 3), color='r')
4433+
ax.add_patch(circ)
4434+
ax.grid(color='c', linestyle='-', linewidth=3)
4435+
ax.tick_params(top=False, bottom=False,
4436+
left=False, right=False)
4437+
for spine in ax.spines.values():
4438+
spine.set_visible(False)
4439+
ax.set_axisbelow(setting)
4440+
4441+
44204442
if __name__ == '__main__':
44214443
import nose
44224444
import sys

matplotlibrc.template

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ backend : $TEMPLATE_BACKEND
301301
#axes.labelpad : 5.0 # space between label and axis
302302
#axes.labelweight : normal # weight of the x and y labels
303303
#axes.labelcolor : black
304-
#axes.axisbelow : False # whether axis gridlines and ticks are below
305-
# the axes elements (lines, text, etc)
304+
#axes.axisbelow : 'line' # draw axis gridlines and ticks below
305+
# patches (True); above patches but below
306+
# lines ('line'); or above all (False)
306307

307308
#axes.formatter.limits : -7, 7 # use scientific notation if log10
308309
# of the axis range is smaller than the

0 commit comments

Comments
 (0)