Skip to content

Commit 36f5306

Browse files
committed
Move the common implementation of Axes.set_x/y/zscale to Axis.
... together with an _axis_method_wrapper. It may be worth trying to figure out later whether Axis._set_scale and Axis._set_axes_scale need to have subtly different behaviors...
1 parent 446de7b commit 36f5306

File tree

3 files changed

+50
-103
lines changed

3 files changed

+50
-103
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3681,47 +3681,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
36813681
return self.xaxis._set_lim(left, right, emit=emit, auto=auto)
36823682

36833683
get_xscale = _axis_method_wrapper("xaxis", "get_scale")
3684-
3685-
def set_xscale(self, value, **kwargs):
3686-
"""
3687-
Set the x-axis scale.
3688-
3689-
Parameters
3690-
----------
3691-
value : {"linear", "log", "symlog", "logit", ...} or `.ScaleBase`
3692-
The axis scale type to apply.
3693-
3694-
**kwargs
3695-
Different keyword arguments are accepted, depending on the scale.
3696-
See the respective class keyword arguments:
3697-
3698-
- `matplotlib.scale.LinearScale`
3699-
- `matplotlib.scale.LogScale`
3700-
- `matplotlib.scale.SymmetricalLogScale`
3701-
- `matplotlib.scale.LogitScale`
3702-
- `matplotlib.scale.FuncScale`
3703-
3704-
Notes
3705-
-----
3706-
By default, Matplotlib supports the above mentioned scales.
3707-
Additionally, custom scales may be registered using
3708-
`matplotlib.scale.register_scale`. These scales can then also
3709-
be used here.
3710-
"""
3711-
old_default_lims = (self.xaxis.get_major_locator()
3712-
.nonsingular(-np.inf, np.inf))
3713-
g = self.get_shared_x_axes()
3714-
for ax in g.get_siblings(self):
3715-
ax.xaxis._set_scale(value, **kwargs)
3716-
ax._update_transScale()
3717-
ax.stale = True
3718-
new_default_lims = (self.xaxis.get_major_locator()
3719-
.nonsingular(-np.inf, np.inf))
3720-
if old_default_lims != new_default_lims:
3721-
# Force autoscaling now, to take advantage of the scale locator's
3722-
# nonsingular() before it possibly gets swapped out by the user.
3723-
self.autoscale_view(scaley=False)
3724-
3684+
set_xscale = _axis_method_wrapper("xaxis", "_set_axes_scale")
37253685
get_xticks = _axis_method_wrapper("xaxis", "get_ticklocs")
37263686
set_xticks = _axis_method_wrapper("xaxis", "set_ticks")
37273687
get_xmajorticklabels = _axis_method_wrapper("xaxis", "get_majorticklabels")
@@ -3953,47 +3913,7 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False,
39533913
return self.yaxis._set_lim(bottom, top, emit=emit, auto=auto)
39543914

39553915
get_yscale = _axis_method_wrapper("yaxis", "get_scale")
3956-
3957-
def set_yscale(self, value, **kwargs):
3958-
"""
3959-
Set the y-axis scale.
3960-
3961-
Parameters
3962-
----------
3963-
value : {"linear", "log", "symlog", "logit", ...} or `.ScaleBase`
3964-
The axis scale type to apply.
3965-
3966-
**kwargs
3967-
Different keyword arguments are accepted, depending on the scale.
3968-
See the respective class keyword arguments:
3969-
3970-
- `matplotlib.scale.LinearScale`
3971-
- `matplotlib.scale.LogScale`
3972-
- `matplotlib.scale.SymmetricalLogScale`
3973-
- `matplotlib.scale.LogitScale`
3974-
- `matplotlib.scale.FuncScale`
3975-
3976-
Notes
3977-
-----
3978-
By default, Matplotlib supports the above mentioned scales.
3979-
Additionally, custom scales may be registered using
3980-
`matplotlib.scale.register_scale`. These scales can then also
3981-
be used here.
3982-
"""
3983-
old_default_lims = (self.yaxis.get_major_locator()
3984-
.nonsingular(-np.inf, np.inf))
3985-
g = self.get_shared_y_axes()
3986-
for ax in g.get_siblings(self):
3987-
ax.yaxis._set_scale(value, **kwargs)
3988-
ax._update_transScale()
3989-
ax.stale = True
3990-
new_default_lims = (self.yaxis.get_major_locator()
3991-
.nonsingular(-np.inf, np.inf))
3992-
if old_default_lims != new_default_lims:
3993-
# Force autoscaling now, to take advantage of the scale locator's
3994-
# nonsingular() before it possibly gets swapped out by the user.
3995-
self.autoscale_view(scalex=False)
3996-
3916+
set_yscale = _axis_method_wrapper("yaxis", "_set_axes_scale")
39973917
get_yticks = _axis_method_wrapper("yaxis", "get_ticklocs")
39983918
set_yticks = _axis_method_wrapper("yaxis", "set_ticks")
39993919
get_ymajorticklabels = _axis_method_wrapper("yaxis", "get_majorticklabels")

lib/matplotlib/axis.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,50 @@ def _set_scale(self, value, **kwargs):
780780
self.isDefault_majfmt = True
781781
self.isDefault_minfmt = True
782782

783+
# This method is directly wrapped by Axes.set_{x,y}scale.
784+
def _set_axes_scale(self, value, **kwargs):
785+
"""
786+
Set this Axis' scale.
787+
788+
Parameters
789+
----------
790+
value : {"linear", "log", "symlog", "logit", ...} or `.ScaleBase`
791+
The axis scale type to apply.
792+
793+
**kwargs
794+
Different keyword arguments are accepted, depending on the scale.
795+
See the respective class keyword arguments:
796+
797+
- `matplotlib.scale.LinearScale`
798+
- `matplotlib.scale.LogScale`
799+
- `matplotlib.scale.SymmetricalLogScale`
800+
- `matplotlib.scale.LogitScale`
801+
- `matplotlib.scale.FuncScale`
802+
803+
Notes
804+
-----
805+
By default, Matplotlib supports the above mentioned scales.
806+
Additionally, custom scales may be registered using
807+
`matplotlib.scale.register_scale`. These scales can then also
808+
be used here.
809+
"""
810+
name, = [name for name, axis in self.axes._axis_map.items()
811+
if axis is self] # The axis name.
812+
old_default_lims = (self.get_major_locator()
813+
.nonsingular(-np.inf, np.inf))
814+
g = self.axes._shared_axes[name]
815+
for ax in g.get_siblings(self.axes):
816+
ax._axis_map[name]._set_scale(value, **kwargs)
817+
ax._update_transScale()
818+
ax.stale = True
819+
new_default_lims = (self.get_major_locator()
820+
.nonsingular(-np.inf, np.inf))
821+
if old_default_lims != new_default_lims:
822+
# Force autoscaling now, to take advantage of the scale locator's
823+
# nonsingular() before it possibly gets swapped out by the user.
824+
self.axes.autoscale_view(
825+
**{f"scale{k}": k == name for k in self.axes._axis_names})
826+
783827
def limit_range_for_scale(self, vmin, vmax):
784828
return self._scale.limit_range_for_scale(vmin, vmax, self.get_minpos())
785829

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -679,27 +679,10 @@ def get_zscale(self):
679679
""" % (", ".join(mscale.get_scale_names()))
680680
return self.zaxis.get_scale()
681681

682-
# We need to slightly redefine these to pass scalez=False
683-
# to their calls of autoscale_view.
684-
685-
def set_xscale(self, value, **kwargs):
686-
self.xaxis._set_scale(value, **kwargs)
687-
self.autoscale_view(scaley=False, scalez=False)
688-
self._update_transScale()
689-
self.stale = True
690-
691-
def set_yscale(self, value, **kwargs):
692-
self.yaxis._set_scale(value, **kwargs)
693-
self.autoscale_view(scalex=False, scalez=False)
694-
self._update_transScale()
695-
self.stale = True
696-
697-
def set_zscale(self, value, **kwargs):
698-
self.zaxis._set_scale(value, **kwargs)
699-
self.autoscale_view(scalex=False, scaley=False)
700-
self._update_transScale()
701-
self.stale = True
702-
682+
# Redefine all three methods to overwrite their docstrings.
683+
set_xscale = _axis_method_wrapper("xaxis", "_set_axes_scale")
684+
set_yscale = _axis_method_wrapper("yaxis", "_set_axes_scale")
685+
set_zscale = _axis_method_wrapper("zaxis", "_set_axes_scale")
703686
set_xscale.__doc__, set_yscale.__doc__, set_zscale.__doc__ = map(
704687
"""
705688
Set the {}-axis scale.

0 commit comments

Comments
 (0)