8000 Merge pull request #18769 from anntzer/gridvisible · matplotlib/matplotlib@d7fc51b · GitHub
[go: up one dir, main page]

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 d7fc51b

Browse files
authored
Merge pull request #18769 from anntzer/gridvisible
Support `ax.grid(visible=<bool>)`.
2 parents a32417f + 28e097e commit d7fc51b

File tree

4 files changed

+59
-48
lines changed

4 files changed

+59
-48
lines changed

lib/matplotlib/axes/_base.py

Lines 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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,10 @@ def clear(self):
782782
self.callbacks = cbook.CallbackRegistry()
783783

784784
# whether the grids are on
785-
self._gridOnMajor = (
785+
self._major_tick_kw['gridOn'] = (
786786
mpl.rcParams['axes.grid'] and
787787
mpl.rcParams['axes.grid.which'] in ('both', 'major'))
788-
self._gridOnMinor = (
788+
self._minor_tick_kw['gridOn'] = (
789789
mpl.rcParams['axes.grid'] and
790790
mpl.rcParams['axes.grid.which'] in ('both', 'minor'))
791791

@@ -1390,7 +1390,6 @@ def get_major_ticks(self, numticks=None):
13901390
# Update the new tick label properties from the old.
13911391
tick = self._get_tick(major=True)
13921392
self.majorTicks.append(tick)
1393-
tick.gridline.set_visible(self._gridOnMajor)
13941393
self._copy_tick_props(self.majorTicks[0], tick)
13951394

13961395
return self.majorTicks[:numticks]
@@ -1404,7 +1403,6 @@ def get_minor_ticks(self, numticks=None):
14041403
# Update the new tick label properties from the old.
14051404
tick = self._get_tick(major=False)
14061405
self.minorTicks.append(tick)
1407-
tick.gridline.set_visible(self._gridOnMinor)
14081406
self._copy_tick_props(self.minorTicks[0], tick)
14091407

14101408
return self.minorTicks[:numticks]
@@ -1429,32 +1427,37 @@ def grid(self, b=None, which='major', **kwargs):
14291427
Define the line properties of the grid, e.g.::
14301428
14311429
grid(color='r', linestyle='-', linewidth=2)
1432-
14331430
"""
1434-
if len(kwargs):
1435-
if not b and b is not None: # something false-like but not None
1431+
if b is not None:
1432+
if 'visible' in kwargs and bool(b) != bool(kwargs['visible']):
1433+
raise ValueError(
1434+
"'b' and 'visible' specify inconsistent grid visibilities")
1435+
if kwargs and not b: # something false-like but not None
14361436
cbook._warn_external('First parameter to grid() is false, '
14371437
'but line properties are supplied. The '
14381438
'grid will be enabled.')
1439-
b = True
1439+
b = True
14401440
which = which.lower()
14411441
_api.check_in_list(['major', 'minor', 'both'], which=which)
14421442
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
1443+
if 'grid_visible' in gridkw:
1444+
forced_visibility = True
1445+
gridkw['gridOn'] = gridkw.pop('grid_visible')
1446+
else:
1447+
forced_visibility = False
14431448

14441449
if which in ['minor', 'both']:
1445-
if b is None:
1446-
self._gridOnMinor = not self._gridOnMinor
1447-
else:
1448-
self._gridOnMinor = b
1449-
self.set_tick_params(which='minor', gridOn=self._gridOnMinor,
1450-
**gridkw)
1450+
if b is None and not forced_visibility:
1451+
gridkw['gridOn'] = not self._minor_tick_kw['gridOn']
1452+
elif b is not None:
1453+
gridkw['gridOn'] = b
1454+
self.set_tick_params(which='minor', **gridkw)
14511455
if which in ['major', 'both']:
1452-
if b is None:
1453-
self._gridOnMajor = not self._gridOnMajor
1454-
else:
1455-
self._gridOnMajor = b
1456-
self.set_tick_params(which='major', gridOn=self._gridOnMajor,
1457-
**gridkw)
1456+
if b is None and not forced_visibility:
1457+
gridkw['gridOn'] = not self._major_tick_kw['gridOn']
1458+
elif b is not None:
1459+
gridkw['gridOn'] = b
1460+
self.set_tick_params(which='major', **gridkw)
14581461
self.stale = True
14591462

14601463
def update_units(self, data):

lib/matplotlib/tests/test_axes.py

Lines 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

Lines 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