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

Skip to content

Commit 2ec3780

Browse files
committed
Merge pull request #6287 from efiring/axisbelow-lines
ENH: add axisbelow option 'line', make it the default
1 parent b12cadd commit 2ec3780

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
@@ -2324,12 +2326,16 @@ def draw(self, renderer=None, inframe=False):
23242326
artists.remove(spine)
23252327

23262328
if self.axison and not inframe:
2327-
if self._axisbelow:
2329+
if self._axisbelow is True:
23282330
self.xaxis.set_zorder(0.5)
23292331
self.yaxis.set_zorder(0.5)
2330-
else:
2332+
elif self._axisbelow is False:
23312333
self.xaxis.set_zorder(2.5)
23322334
self.yaxis.set_zorder(2.5)
2335+
else:
2336+
# 'line': above patches, below lines
2337+
self.xaxis.set_zorder(1.5)
2338+
self.yaxis.set_zorder(1.5)
23332339
else:
23342340
for _axis in self._get_axis_list():
23352341
artists.remove(_axis)
@@ -2429,9 +2435,9 @@ def set_axisbelow(self, b):
24292435
Set whether the axis ticks and gridlines are above or below most
24302436
artists
24312437
2432-
ACCEPTS: [ *True* | *False* ]
2438+
ACCEPTS: [ *True* | *False* | 'line' ]
24332439
"""
2434-
self._axisbelow = b
2440+
self._axisbelow = validate_axisbelow(b)
24352441
self.stale = True
24362442

24372443
@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"""
@@ -987,7 +998,7 @@ def validate_hist_bins(s):
987998
'errorbar.capsize': [0, validate_float],
988999

9891000
# axes props
990-
'axes.axisbelow': [False, validate_bool],
1001+
'axes.axisbelow': ['line', validate_axisbelow],
9911002
'axes.hold': [True, validate_bool],
9921003
'axes.facecolor': ['w', validate_color], # background color; white
9931004
'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
@@ -4415,6 +4416,27 @@ def test_date_timezone_x_and_y():
44154416
plt.plot_date(time_index, time_index, tz='US/Eastern', ydate=True)
44164417

44174418

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