Skip to content

Commit 2d5a8ec

Browse files
authored
Merge pull request #23500 from anntzer/set_fooscale
MNT: Move the common implementation of Axes.set_x/y/zscale to Axis.
2 parents a56afb3 + 36f5306 commit 2d5a8ec

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
@@ -3669,47 +3669,7 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False,
36693669
return self.xaxis._set_lim(left, right, emit=emit, auto=auto)
36703670

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

39433903
get_yscale = _axis_method_wrapper("yaxis", "get_scale")
3944-
3945-
def set_yscale(self, value, **kwargs):
3946-
"""
3947-
Set the y-axis scale.
3948-
3949-
Parameters
3950-
----------
3951-
value : {"linear", "log", "symlog", "logit", ...} or `.ScaleBase`
3952-
The axis scale type to apply.
3953-
3954-
**kwargs
3955-
Different keyword arguments are accepted, depending on the scale.
3956-
See the respective class keyword arguments:
3957-
3958-
- `matplotlib.scale.LinearScale`
3959-
- `matplotlib.scale.LogScale`
3960-
- `matplotlib.scale.SymmetricalLogScale`
3961-
- `matplotlib.scale.LogitScale`
3962-
- `matplotlib.scale.FuncScale`
3963-
3964-
Notes
3965-
-----
3966-
By default, Matplotlib supports the above mentioned scales.
3967-
Additionally, custom scales may be registered using
3968-
`matplotlib.scale.register_scale`. These scales can then also
3969-
be used here.
3970-
"""
3971-
old_default_lims = (self.yaxis.get_major_locator()
3972-
.nonsingular(-np.inf, np.inf))
3973-
g = self.get_shared_y_axes()
3974-
for ax in g.get_siblings(self):
3975-
ax.yaxis._set_scale(value, **kwargs)
3976-
ax._update_transScale()
3977-
ax.stale = True
3978-
new_default_lims = (self.yaxis.get_major_locator()
3979-
.nonsingular(-np.inf, np.inf))
3980-
if old_default_lims != new_default_lims:
3981-
# Force autoscaling now, to take advantage of the scale locator's
3982-
# nonsingular() before it possibly gets swapped out by the user.
3983-
self.autoscale_view(scalex=False)
3984-
3904+
set_yscale = _axis_method_wrapper("yaxis", "_set_axes_scale")
39853905
get_yticks = _axis_method_wrapper("yaxis", "get_ticklocs")
39863906
set_yticks = _axis_method_wrapper("yaxis", "set_ticks")
39873907
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
@@ -779,6 +779,50 @@ def _set_scale(self, value, **kwargs):
779779
self.isDefault_majfmt = True
780780
self.isDefault_minfmt = True
781781

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

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -673,27 +673,10 @@ def get_zlim(self):
673673

674674
get_zscale = _axis_method_wrapper("zaxis", "get_scale")
675675

676-
# We need to slightly redefine these to pass scalez=False
677-
# to their calls of autoscale_view.
678-
679-
def set_xscale(self, value, **kwargs):
680-
self.xaxis._set_scale(value, **kwargs)
681-
self.autoscale_view(scaley=False, scalez=False)
682-
self._update_transScale()
683-
self.stale = True
684-
685-
def set_yscale(self, value, **kwargs):
686-
self.yaxis._set_scale(value, **kwargs)
687-
self.autoscale_view(scalex=False, scalez=False)
688-
self._update_transScale()
689-
self.stale = True
690-
691-
def set_zscale(self, value, **kwargs):
692-
self.zaxis._set_scale(value, **kwargs)
693-
self.autoscale_view(scalex=False, scaley=False)
694-
self._update_transScale()
695-
self.stale = True
696-
676+
# Redefine all three methods to overwrite their docstrings.
677+
set_xscale = _axis_method_wrapper("xaxis", "_set_axes_scale")
678+
set_yscale = _axis_method_wrapper("yaxis", "_set_axes_scale")
679+
set_zscale = _axis_method_wrapper("zaxis", "_set_axes_scale")
697680
set_xscale.__doc__, set_yscale.__doc__, set_zscale.__doc__ = map(
698681
"""
699682
Set the {}-axis scale.

0 commit comments

Comments
 (0)