Skip to content

Rename (with deprecation) first parameter of grid() from b to visible. #20173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/deprecations/20173-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The first parameter of ``Axes.grid`` and ``Axis.grid`` has been renamed to *visible*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The parameter was previously named *b*. This deprecation only matters if
that parameter was passed using a keyword argument, e.g. ``grid(b=False)``.
15 changes: 8 additions & 7 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3191,17 +3191,18 @@ def set_axisbelow(self, b):
self.stale = True

@docstring.dedent_interpd
def grid(self, b=None, which='major', axis='both', **kwargs):
@_api.rename_parameter("3.5", "b", "visible")
def grid(self, visible=None, which='major', axis='both', **kwargs):
"""
Configure the grid lines.

Parameters
----------
b : bool or None, optional
Whether to show the grid lines. If any *kwargs* are supplied,
it is assumed you want the grid on and *b* will be set to True.
visible : bool or None, optional
Whether to show the grid lines. If any *kwargs* are supplied, it
is assumed you want the grid on and *visible* will be set to True.

If *b* is *None* and there are no *kwargs*, this toggles the
If *visible* is *None* and there are no *kwargs*, this toggles the
visibility of the lines.

which : {'major', 'minor', 'both'}, optional
Expand Down Expand Up @@ -3229,9 +3230,9 @@ def grid(self, b=None, which='major', axis='both', **kwargs):
"""
_api.check_in_list(['x', 'y', 'both'], axis=axis)
if axis in ['x', 'both']:
self.xaxis.grid(b, which=which, **kwargs)
self.xaxis.grid(visible, which=which, **kwargs)
if axis in ['y', 'both']:
self.yaxis.grid(b, which=which, **kwargs)
self.yaxis.grid(visible, which=which, **kwargs)

def ticklabel_format(self, *, axis='both', style='', scilimits=None,
useOffset=None, useLocale=None, useMathText=None):
Expand Down
41 changes: 13 additions & 28 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,17 +1398,18 @@ def get_minor_ticks(self, numticks=None):

return self.minorTicks[:numticks]

def grid(self, b=None, which='major', **kwargs):
@_api.rename_parameter("3.5", "b", "visible")
def grid(self, visible=None, which='major', **kwargs):
"""
Configure the grid lines.

Parameters
----------
b : bool or None
Whether to show the grid lines. If any *kwargs* are supplied,
it is assumed you want the grid on and *b* will be set to True.
visible : bool or None
Whether to show the grid lines. If any *kwargs* are supplied, it
is assumed you want the grid on and *visible* will be set to True.

If *b* is *None* and there are no *kwargs*, this toggles the
If *visible* is *None* and there are no *kwargs*, this toggles the
visibility of the lines.

which : {'major', 'minor', 'both'}
Expand All @@ -1419,40 +1420,24 @@ def grid(self, b=None, which='major', **kwargs):

grid(color='r', linestyle='-', linewidth=2)
"""
TOGGLE = object()
UNSET = object()
visible = kwargs.pop('visible', UNSET)

if b is None:
if visible is UNSET:
if kwargs: # grid(color='r')
b = True
else: # grid()
b = TOGGLE
else: # grid(visible=v)
b = visible
else:
if visible is not UNSET and bool(b) != bool(visible):
# grid(True, visible=False), grid(False, visible=True)
raise ValueError(
"'b' and 'visible' specify inconsistent grid visibilities")
if kwargs and not b: # something false-like but not None
# grid(0, visible=True)
if kwargs:
if visible is None:
visible = True
elif not visible: # something false-like but not None
_api.warn_external('First parameter to grid() is false, '
'but line properties are supplied. The '
'grid will be enabled.')
b = True

visible = True
which = which.lower()
_api.check_in_list(['major', 'minor', 'both'], which=which)
gridkw = {'grid_' + item[0]: item[1] for item in kwargs.items()}
if which in ['minor', 'both']:
gridkw['gridOn'] = (not self._minor_tick_kw['gridOn']
if b is TOGGLE else b)
if visible is None else visible)
self.set_tick_params(which='minor', **gridkw)
if which in ['major', 'both']:
gridkw['gridOn'] = (not self._major_tick_kw['gridOn']
if b is TOGGLE else b)
if visible is None else visible)
self.set_tick_params(which='major', **gridkw)
self.stale = True

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2813,8 +2813,8 @@ def fill_betweenx(

# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
@_copy_docstring_and_deprecators(Axes.grid)
def grid(b=None, which='major', axis='both', **kwargs):
return gca().grid(b=b, which=which, axis=axis, **kwargs)
def grid(visible=None, which='major', axis='both', **kwargs):
return gca().grid(visible=visible, which=which, axis=axis, **kwargs)


# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
Expand Down
17 changes: 9 additions & 8 deletions lib/mpl_toolkits/axisartist/axislines.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,22 +528,23 @@ def cla(self):
def get_grid_helper(self):
return self._grid_helper

def grid(self, b=None, which='major', axis="both", **kwargs):
@_api.rename_parameter("3.5", "b", "visible")
def grid(self, visible=None, which='major', axis="both", **kwargs):
"""
Toggle the gridlines, and optionally set the properties of the lines.
"""
# There are some discrepancies in the behavior of grid() between
# axes_grid and Matplotlib, because axes_grid explicitly sets the
# visibility of the gridlines.
super().grid(b, which=which, axis=axis, **kwargs)
super().grid(visible, which=which, axis=axis, **kwargs)
if not self._axisline_on:
return
if b is None:
b = (self.axes.xaxis._minor_tick_kw["gridOn"]
or self.axes.xaxis._major_tick_kw["gridOn"]
or self.axes.yaxis._minor_tick_kw["gridOn"]
or self.axes.yaxis._major_tick_kw["gridOn"])
self.gridlines.set(which=which, axis=axis, visible=b)
if visible is None:
visible = (self.axes.xaxis._minor_tick_kw["gridOn"]
or self.axes.xaxis._major_tick_kw["gridOn"]
or self.axes.yaxis._minor_tick_kw["gridOn"]
or self.axes.yaxis._major_tick_kw["gridOn"])
self.gridlines.set(which=which, axis=axis, visible=visible)
self.gridlines.set(**kwargs)

def get_children(self):
Expand Down
7 changes: 4 additions & 3 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,8 @@ def set_frame_on(self, b):
self._frameon = bool(b)
self.stale = True

def grid(self, b=True, **kwargs):
@_api.rename_parameter("3.5", "b", "visible")
def grid(self, visible=True, **kwargs):
"""
Set / unset 3D grid.

Expand All @@ -1383,8 +1384,8 @@ def grid(self, b=True, **kwargs):
"""
# TODO: Operate on each axes separately
if len(kwargs):
b = True
self._draw_grid = b
visible = True
self._draw_grid = visible
self.stale = True

def locator_params(self, axis='both', tight=None, **kwargs):
Expand Down