Skip to content

Move the common implementation of Axes.set_x/y/zscale to Axis. #23500

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
Aug 3, 2022
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
84 changes: 2 additions & 82 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3681,47 +3681,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
return self.xaxis._set_lim(left, right, emit=emit, auto=auto)

get_xscale = _axis_method_wrapper("xaxis", "get_scale")

def set_xscale(self, value, **kwargs):
"""
Set the x-axis scale.

Parameters
----------
value : {"linear", "log", "symlog", "logit", ...} or `.ScaleBase`
The axis scale type to apply.

**kwargs
Different keyword arguments are accepted, depending on the scale.
See the respective class keyword arguments:

- `matplotlib.scale.LinearScale`
- `matplotlib.scale.LogScale`
- `matplotlib.scale.SymmetricalLogScale`
- `matplotlib.scale.LogitScale`
- `matplotlib.scale.FuncScale`

Notes
-----
By default, Matplotlib supports the above mentioned scales.
Additionally, custom scales may be registered using
`matplotlib.scale.register_scale`. These scales can then also
be used here.
"""
old_default_lims = (self.xaxis.get_major_locator()
.nonsingular(-np.inf, np.inf))
g = self.get_shared_x_axes()
for ax in g.get_siblings(self):
ax.xaxis._set_scale(value, **kwargs)
ax._update_transScale()
ax.stale = True
new_default_lims = (self.xaxis.get_major_locator()
.nonsingular(-np.inf, np.inf))
if old_default_lims != new_default_lims:
# Force autoscaling now, to take advantage of the scale locator's
# nonsingular() before it possibly gets swapped out by the user.
self.autoscale_view(scaley=False)

set_xscale = _axis_method_wrapper("xaxis", "_set_axes_scale")
get_xticks = _axis_method_wrapper("xaxis", "get_ticklocs")
set_xticks = _axis_method_wrapper("xaxis", "set_ticks")
get_xmajorticklabels = _axis_method_wrapper("xaxis", "get_majorticklabels")
Expand Down Expand Up @@ -3953,47 +3913,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
return self.yaxis._set_lim(bottom, top, emit=emit, auto=auto)

get_yscale = _axis_method_wrapper("yaxis", "get_scale")

def set_yscale(self, value, **kwargs):
"""
Set the y-axis scale.

Parameters
----------
value : {"linear", "log", "symlog", "logit", ...} or `.ScaleBase`
The axis scale type to apply.

**kwargs
Different keyword arguments are accepted, depending on the scale.
See the respective class keyword arguments:

- `matplotlib.scale.LinearScale`
- `matplotlib.scale.LogScale`
- `matplotlib.scale.SymmetricalLogScale`
- `matplotlib.scale.LogitScale`
- `matplotlib.scale.FuncScale`

Notes
-----
By default, Matplotlib supports the above mentioned scales.
Additionally, custom scales may be registered using
`matplotlib.scale.register_scale`. These scales can then also
be used here.
"""
old_default_lims = (self.yaxis.get_major_locator()
.nonsingular(-np.inf, np.inf))
g = self.get_shared_y_axes()
for ax in g.get_siblings(self):
ax.yaxis._set_scale(value, **kwargs)
ax._update_transScale()
ax.stale = True
new_default_lims = (self.yaxis.get_major_locator()
.nonsingular(-np.inf, np.inf))
if old_default_lims != new_default_lims:
# Force autoscaling now, to take advantage of the scale locator's
# nonsingular() before it possibly gets swapped out by the user.
self.autoscale_view(scalex=False)

set_yscale = _axis_method_wrapper("yaxis", "_set_axes_scale")
get_yticks = _axis_method_wrapper("yaxis", "get_ticklocs")
set_yticks = _axis_method_wrapper("yaxis", "set_ticks")
get_ymajorticklabels = _axis_method_wrapper("yaxis", "get_majorticklabels")
Expand Down
44 changes: 44 additions & 0 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,50 @@ def _set_scale(self, value, **kwargs):
self.isDefault_majfmt = True
self.isDefault_minfmt = True

# This method is directly wrapped by Axes.set_{x,y}scale.
def _set_axes_scale(self, value, **kwargs):
"""
Set this Axis' scale.

Parameters
----------
value : {"linear", "log", "symlog", "logit", ...} or `.ScaleBase`
The axis scale type to apply.

**kwargs
Different keyword arguments are accepted, depending on the scale.
See the respective class keyword arguments:

- `matplotlib.scale.LinearScale`
- `matplotlib.scale.LogScale`
- `matplotlib.scale.SymmetricalLogScale`
- `matplotlib.scale.LogitScale`
- `matplotlib.scale.FuncScale`

Notes
-----
By default, Matplotlib supports the above mentioned scales.
Additionally, custom scales may be registered using
`matplotlib.scale.register_scale`. These scales can then also
be used here.
"""
name, = [name for name, axis in self.axes._axis_map.items()
if axis is self] # The axis name.
old_default_lims = (self.get_major_locator()
.nonsingular(-np.inf, np.inf))
g = self.axes._shared_axes[name]
for ax in g.get_siblings(self.axes):
ax._axis_map[name]._set_scale(value, **kwargs)
ax._update_transScale()
ax.stale = True
new_default_lims = (self.get_major_locator()
.nonsingular(-np.inf, np.inf))
if old_default_lims != new_default_lims:
# Force autoscaling now, to take advantage of the scale locator's
# nonsingular() before it possibly gets swapped out by the user.
self.axes.autoscale_view(
**{f"scale{k}": k == name for k in self.axes._axis_names})

def limit_range_for_scale(self, vmin, vmax):
return self._scale.limit_range_for_scale(vmin, vmax, self.get_minpos())

Expand Down
25 changes: 4 additions & 21 deletions lib/mpl_toolkits/mplot3d/axes3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,27 +679,10 @@ def get_zscale(self):
""" % (", ".join(mscale.get_scale_names()))
return self.zaxis.get_scale()

# We need to slightly redefine these to pass scalez=False
# to their calls of autoscale_view.

def set_xscale(self, value, **kwargs):
self.xaxis._set_scale(value, **kwargs)
self.autoscale_view(scaley=False, scalez=False)
self._update_transScale()
self.stale = True

def set_yscale(self, value, **kwargs):
self.yaxis._set_scale(value, **kwargs)
self.autoscale_view(scalex=False, scalez=False)
self._update_transScale()
self.stale = True

def set_zscale(self, value, **kwargs):
self.zaxis._set_scale(value, **kwargs)
self.autoscale_view(scalex=False, scaley=False)
self._update_transScale()
self.stale = True

# Redefine all three methods to overwrite their docstrings.
set_xscale = _axis_method_wrapper("xaxis", "_set_axes_scale")
set_yscale = _axis_method_wrapper("yaxis", "_set_axes_scale")
set_zscale = _axis_method_wrapper("zaxis", "_set_axes_scale")
set_xscale.__doc__, set_yscale.__doc__, set_zscale.__doc__ = map(
"""
Set the {}-axis scale.
Expand Down