Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 28e097e

Browse filesBrowse files
committed
Support ax.grid(visible=<bool>).
Remove the separate _gridOnMajor/_gridOnMinor attributes and store visibility together with all other grid attributes in the _major_tick_kw/_minor_tick_kw dicts under the "gridOn" key. This makes it possible to update just that entry in `Axis.grid`. Also, don't normalize `b` to True `if len(kwargs)` in `Axes.grid`: we need to keep it as `None`, normalizing it only to True/False in `Axis.grid` (which handles that just fine), so that `Axis.grid` can handle the consistency checks between `b` and `visible`.
1 parent 1f8f717 commit 28e097e
Copy full SHA for 28e097e

File tree

Expand file treeCollapse file tree

4 files changed

+59
-48
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+59
-48
lines changed

‎lib/matplotlib/axes/_base.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_base.py
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,8 +2976,6 @@ def grid(self, b=None, which='major', axis='both', **kwargs):
29762976
use `.set_axisbelow` or, for more control, call the
29772977
`~.Artist.set_zorder` method of each axis.
29782978
"""
2979-
if len(kwargs):
2980-
b = True
29812979
_api.check_in_list(['x', 'y', 'both'], axis=axis)
29822980
if axis in ['x', 'both']:
29832981
self.xaxis.grid(b, which=which, **kwargs)

‎lib/matplotlib/axis.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axis.py
+23-20Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -790,10 +790,10 @@ def clear(self):
790790
self.callbacks = cbook.CallbackRegistry()
791791

792792
# whether the grids are on
793-
self._gridOnMajor = (
793+
self._major_tick_kw['gridOn'] = (
794794
mpl.rcParams['axes.grid'] and
795795
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
796-
self._gridOnMinor = (
796+
self._minor_tick_kw['gridOn'] = (
797797
mpl.rcParams['axes.grid'] and
798798
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
799799

@@ -1398,7 +1398,6 @@ def get_major_ticks(self, numticks=None):
13981398
# Update the new tick label properties from the old.
13991399
tick = self._get_tick(major=True)
14001400
self.majorTicks.append(tick)
1401-
tick.gridline.set_visible(self._gridOnMajor)
14021401
self._copy_tick_props(self.majorTicks[0], tick)
14031402

14041403
return self.majorTicks[:numticks]
@@ -1412,7 +1411,6 @@ def get_minor_ticks(self, numticks=None):
14121411
# Update the new tick label properties from the old.
14131412
tick = self._get_tick(major=False)
14141413
self.minorTicks.append(tick)
1415-
tick.gridline.set_visible(self._gridOnMinor)
14161414
self._copy_tick_props(self.minorTicks[0], tick)
14171415

14181416
return self.minorTicks[:numticks]
@@ -1437,32 +1435,37 @@ def grid(self, b=None, which='major', **kwargs):
14371435
Define the line properties of the grid, e.g.::
14381436
14391437
grid(color='r', linestyle='-', linewidth=2)
1440-
14411438
"""
1442-
if len(kwargs):
1443-
if not b and b is not None: # something false-like but not None
1439+
if b is not None:
1440+
if 'visible' in kwargs and bool(b) != bool(kwargs['visible']):
1441+
raise ValueError(
1442+
"'b' and 'visible' specify inconsistent grid visibilities")
1443+
if kwargs and not b: # something false-like but not None
14441444
cbook._warn_external('First parameter to grid() is false, '
14451445
'but line properties are supplied. The '
14461446
'grid will be enabled.')
1447-
b = True
1447+
b = True
14481448
which = which.lower()
14491449
_api.check_in_list(['major', 'minor', 'both'], which=which)
14501450
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
1451+
if 'grid_visible' in gridkw:
1452+
forced_visibility = True
1453+
gridkw['gridOn'] = gridkw.pop('grid_visible')
1454+
else:
1455+
forced_visibility = False
14511456

14521457
if which in ['minor', 'both']:
1453-
if b is None:
1454-
self._gridOnMinor = not self._gridOnMinor
1455-
else:
1456-
self._gridOnMinor = b
1457-
self.set_tick_params(which='minor', gridOn=self._gridOnMinor,
1458-
**gridkw)
1458+
if b is None and not forced_visibility:
1459+
gridkw['gridOn'] = not self._minor_tick_kw['gridOn']
1460+
elif b is not None:
1461+
gridkw['gridOn'] = b
1462+
self.set_tick_params(which='minor', **gridkw)
14591463
if which in ['major', 'both']:
1460-
if b is None:
1461-
self._gridOnMajor = not self._gridOnMajor
1462-
else:
1463-
self._gridOnMajor = b
1464-
self.set_tick_params(which='major', gridOn=self._gridOnMajor,
1465-
**gridkw)
1464+
if b is None and not forced_visibility:
1465+
gridkw['gridOn'] = not self._major_tick_kw['gridOn']
1466+
elif b is not None:
1467+
gridkw['gridOn'] = b
1468+
self.set_tick_params(which='major', **gridkw)
14661469
self.stale = True
14671470

14681471
def update_units(self, data):

‎lib/matplotlib/tests/test_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_axes.py
+27-17Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4577,25 +4577,35 @@ def test_twin_spines_on_top():
45774577
ax2.fill_between("i", "j", color='#7FC97F', alpha=.5, data=data)
45784578

45794579

4580-
def test_rcparam_grid_minor():
4581-
orig_grid = matplotlib.rcParams['axes.grid']
4582-
orig_locator = matplotlib.rcParams['axes.grid.which']
4583-
4584-
matplotlib.rcParams['axes.grid'] = True
4585-
4586-
values = (
4587-
('both', (True, True)),
4588-
('major', (True, False)),
4589-
('minor', (False, True))
4590-
)
4580+
@pytest.mark.parametrize("grid_which, major_visible, minor_visible", [
4581+
("both", True, True),
4582+
("major", True, False),
4583+
("minor", False, True),
4584+
])
4585+
def test_rcparam_grid_minor(grid_which, major_visible, minor_visible):
4586+
mpl.rcParams.update({"axes.grid": True, "axes.grid.which": grid_which})
4587+
fig, ax = plt.subplots()
4588+
fig.canvas.draw()
4589+
assert all(tick.gridline.get_visible() == major_visible
4590+
for tick in ax.xaxis.majorTicks)
4591+
assert all(tick.gridline.get_visible() == minor_visible
4592+
for tick in ax.xaxis.minorTicks)
45914593

4592-
for locator, result in values:
4593-
matplotlib.rcParams['axes.grid.which'] = locator
4594-
fig, ax = plt.subplots()
4595-
assert (ax.xaxis._gridOnMajor, ax.xaxis._gridOnMinor) == result
45964594

4597-
matplotlib.rcParams['axes.grid'] = orig_grid
4598-
matplotlib.rcParams['axes.grid.which'] = orig_locator
4595+
def test_grid():
4596+
fig, ax = plt.subplots()
4597+
ax.grid()
4598+
fig.canvas.draw()
4599+
assert ax.xaxis.majorTicks[0].gridline.get_visible()
4600+
ax.grid(visible=False)
4601+
fig.canvas.draw()
4602+
assert not ax.xaxis.majorTicks[0].gridline.get_visible()
4603+
ax.grid(visible=True)
4604+
fig.canvas.draw()
4605+
assert ax.xaxis.majorTicks[0].gridline.get_visible()
4606+
ax.grid()
4607+
fig.canvas.draw()
4608+
assert not ax.xaxis.majorTicks[0].gridline.get_visible()
45994609

46004610

46014611
def test_vline_limit():

‎lib/mpl_toolkits/axisartist/axislines.py

Copy file name to clipboardExpand all lines: lib/mpl_toolkits/axisartist/axislines.py
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,9 @@ def get_gridlines(self, which="major", axis="both"):
439439
if axis in ["both", "y"]:
440440
x1, x2 = self.axes.get_xlim()
441441
locs = []
442-
if self.axes.yaxis._gridOnMajor:
442+
if self.axes.yaxis._major_tick_kw["gridOn"]:
443443
locs.extend(self.axes.yaxis.major.locator())
444-
if self.axes.yaxis._gridOnMinor:
444+
if self.axes.yaxis._minor_tick_kw["gridOn"]:
445445
locs.extend(self.axes.yaxis.minor.locator())
446446

447447
for y in locs:
@@ -533,17 +533,17 @@ def grid(self, b=None, which='major', axis="both", **kwargs):
533533
"""
534534
Toggle the gridlines, and optionally set the properties of the lines.
535535
"""
536-
# their are some discrepancy between the behavior of grid in
537-
# axes_grid and the original mpl's grid, because axes_grid
538-
# explicitly set the visibility of the gridlines.
536+
# There are some discrepancies in the behavior of grid() between
537+
# axes_grid and Matplotlib, because axes_grid explicitly sets the
538+
# visibility of the gridlines.
539539
super().grid(b, which=which, axis=axis, **kwargs)
540540
if not self._axisline_on:
541541
return
542542
if b is None:
543-
b = (self.axes.xaxis._gridOnMinor
544-
or self.axes.xaxis._gridOnMajor
545-
or self.axes.yaxis._gridOnMinor
546-
or self.axes.yaxis._gridOnMajor)
543+
b = (self.axes.xaxis._minor_tick_kw["gridOn"]
544+
or self.axes.xaxis._major_tick_kw["gridOn"]
545+
or self.axes.yaxis._minor_tick_kw["gridOn"]
546+
or self.axes.yaxis._major_tick_kw["gridOn"])
547547
self.gridlines.set(which=which, axis=axis, visible=b)
548548
self.gridlines.set(**kwargs)
549549

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.