Skip to content

Commit 05e383a

Browse files
committed
Further refactor axes limits setting.
1 parent 268e955 commit 05e383a

File tree

2 files changed

+44
-97
lines changed

2 files changed

+44
-97
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,15 +2817,33 @@ def get_xlim(self):
28172817
"""
28182818
return tuple(self.viewLim.intervalx)
28192819

2820-
def _set_lim(self, axis, low, high, low_name, high_name,
2821-
target_obj, target_attr, emit, auto, kw):
2822-
"""Helper factoring the functionality of `get_{x,y,z}lim`."""
2823-
# Perhaps we can use axis.{get,set}_view_interval()... but zaxis seems
2824-
# to behave differently.
2825-
_called_from_pan = kw.pop("_called_from_pan", False)
2826-
if kw:
2827-
raise ValueError("Unrecognized kwargs: {}".format(kw))
2828-
axis_name = axis.axis_name
2820+
def _set_lim(self, axis, axis_name, low, high, low_name, high_name,
2821+
target_obj, target_attr, emit, auto):
2822+
"""Helper factoring the functionality of ``set_{x,y,z}lim``.
2823+
2824+
Parameters
2825+
----------
2826+
axis : Axis
2827+
The target axis.
2828+
axis_name : str
2829+
The axis name to use in ``get_?lim`` and ``_autoscale?on``. (Not
2830+
``axis.axis_name`` because the name of `.RadianAxes` is "radius",
2831+
not "r". Not using ``axis.{get,set}_view_interval`` because 3D
2832+
axes behave differently.)
2833+
low, high :
2834+
The first two arguments passed to `set_xlim` (and other
2835+
axes). Either both ``Optional[float]``, or a pair of two
2836+
``Optional[float]`` and ``None``.
2837+
low_name, high_name : str
2838+
Names of the "low" and "high" arguments, for generating error
2839+
messages.
2840+
target_obj, target_attr :
2841+
Target object holding the limits. The limits are applied as
2842+
``target_obj.target_attr = low, high`` (after default handling,
2843+
unit conversion, and validation).
2844+
emit, auto :
2845+
See docstring of `set_xlim` (etc.).
2846+
"""
28292847
if high is None and iterable(low):
28302848
low, high = low
28312849
old_low, old_high = getattr(self, "get_{}lim".format(axis_name))()
@@ -2836,8 +2854,7 @@ def _set_lim(self, axis, low, high, low_name, high_name,
28362854
self._process_unit_info(**{"{}data".format(axis_name): [low, high]})
28372855
low, high = map(axis.convert_units, [low, high])
28382856
for limit in [low, high]:
2839-
if not (_called_from_pan or
2840-
np.isreal(limit) and np.isfinite(limit)):
2857+
if not (np.isreal(limit) and np.isfinite(limit)):
28412858
raise ValueError(
28422859
"Axis limits must be (or convert to) finite reals")
28432860
if low == high:
@@ -2928,8 +2945,8 @@ def set_xlim(self, left=None, right=None, emit=True, auto=False, **kw):
29282945
left = kw.pop('xmin')
29292946
if 'xmax' in kw:
29302947
right = kw.pop('xmax')
2931-
return self._set_lim(self.xaxis, left, right, "left", "right",
2932-
self.viewLim, "intervalx", emit, auto, kw)
2948+
return self._set_lim(self.xaxis, "x", left, right, "left", "right",
2949+
self.viewLim, "intervalx", emit, auto, **kw)
29332950

29342951
def get_xscale(self):
29352952
return self.xaxis.get_scale()
@@ -3206,8 +3223,8 @@ def set_ylim(self, bottom=None, top=None, emit=True, auto=False, **kw):
32063223
bottom = kw.pop('ymin')
32073224
if 'ymax' in kw:
32083225
top = kw.pop('ymax')
3209-
return self._set_lim(self.yaxis, bottom, top, "bottom", "top",
3210-
self.viewLim, "intervaly", emit, auto, kw)
3226+
return self._set_lim(self.yaxis, "y", bottom, top, "bottom", "top",
3227+
self.viewLim, "intervaly", emit, auto, **kw)
32113228

32123229
def get_yscale(self):
32133230
return self.yaxis.get_scale()
@@ -3776,8 +3793,12 @@ def format_deltas(key, dx, dy):
37763793
warnings.warn('Overflow while panning')
37773794
return
37783795

3779-
self.set_xlim(*result.intervalx, _called_from_pan=True)
3780-
self.set_ylim(*result.intervaly, _called_from_pan=True)
3796+
valid = np.isfinite(result.transformed(p.trans))
3797+
points = result.get_points().astype(object)
3798+
# Just ignore invalid limits (typically, underflow in log-scale).
3799+
points[~valid] = None
3800+
self.set_xlim(points[:, 0])
3801+
self.set_ylim(points[:, 1])
37813802

37823803
@cbook.deprecated("2.1")
37833804
def get_cursor_props(self):

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -612,45 +612,8 @@ def set_xlim3d(self, left=None, right=None, emit=True, auto=False, **kw):
612612
left = kw.pop('xmin')
613613
if 'xmax' in kw:
614614
right = kw.pop('xmax')
615-
if kw:
616-
raise ValueError("unrecognized kwargs: %s" % list(kw))
617-
618-
if right is None and cbook.iterable(left):
619-
left, right = left
620-
621-
self._process_unit_info(xdata=(left, right))
622-
left = self._validate_converted_limits(left, self.convert_xunits)
623-
right = self._validate_converted_limits(right, self.convert_xunits)
624-
625-
old_left, old_right = self.get_xlim()
626-
if left is None:
627-
left = old_left
628-
if right is None:
629-
right = old_right
630-
631-
if left == right:
632-
warnings.warn(('Attempting to set identical left==right results\n'
633-
'in singular transformations; automatically expanding.\n'
634-
'left=%s, right=%s') % (left, right))
635-
left, right = mtransforms.nonsingular(left, right, increasing=False)
636-
left, right = self.xaxis.limit_range_for_scale(left, right)
637-
self.xy_viewLim.intervalx = (left, right)
638-
639-
if auto is not None:
640-
self._autoscaleXon = bool(auto)
641-
642-
if emit:
643-
self.callbacks.process('xlim_changed', self)
644-
# Call all of the other x-axes that are shared with this one
645-
for other in self._shared_x_axes.get_siblings(self):
646-
if other is not self:
647-
other.set_xlim(self.xy_viewLim.intervalx,
648-
emit=False, auto=auto)
649-
if (other.figure != self.figure and
650-
other.figure.canvas is not None):
651-
other.figure.canvas.draw_idle()
652-
self.stale = True
653-
return left, right
615+
return self._set_lim(self.xaxis, "x", left, right, "left", "right",
616+
self.xy_viewLim, "intervalx", emit, auto, **kw)
654617
set_xlim = set_xlim3d
655618

656619
def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
@@ -664,45 +627,8 @@ def set_ylim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
664627
bottom = kw.pop('ymin')
665628
if 'ymax' in kw:
666629
top = kw.pop('ymax')
667-
if kw:
668-
raise ValueError("unrecognized kwargs: %s" % list(kw))
669-
670-
if top is None and cbook.iterable(bottom):
671-
bottom, top = bottom
672-
673-
self._process_unit_info(ydata=(bottom, top))
674-
bottom = self._validate_converted_limits(bottom, self.convert_yunits)
675-
top = self._validate_converted_limits(top, self.convert_yunits)
676-
677-
old_bottom, old_top = self.get_ylim()
678-
if bottom is None:
679-
bottom = old_bottom
680-
if top is None:
681-
top = old_top
682-
683-
if top == bottom:
684-
warnings.warn(('Attempting to set identical bottom==top results\n'
685-
'in singular transformations; automatically expanding.\n'
686-
'bottom=%s, top=%s') % (bottom, top))
687-
bottom, top = mtransforms.nonsingular(bottom, top, increasing=False)
688-
bottom, top = self.yaxis.limit_range_for_scale(bottom, top)
689-
self.xy_viewLim.intervaly = (bottom, top)
690-
691-
if auto is not None:
692-
self._autoscaleYon = bool(auto)
693-
694-
if emit:
695-
self.callbacks.process('ylim_changed', self)
696-
# Call all of the other y-axes that are shared with this one
697-
for other in self._shared_y_axes.get_siblings(self):
698-
if other is not self:
699-
other.set_ylim(self.xy_viewLim.intervaly,
700-
emit=False, auto=auto)
701-
if (other.figure != self.figure and
702-
other.figure.canvas is not None):
703-
other.figure.canvas.draw_idle()
704-
self.stale = True
705-
return bottom, top
630+
return self._set_lim(self.yaxis, "y", bottom, top, "bottom", "top",
631+
self.xy_viewLim, "intervaly", emit, auto, **kw)
706632
set_ylim = set_ylim3d
707633

708634
def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
@@ -716,8 +642,8 @@ def set_zlim3d(self, bottom=None, top=None, emit=True, auto=False, **kw):
716642
bottom = kw.pop('zmin')
717643
if 'zmax' in kw:
718644
top = kw.pop('zmax')
719-
return self._set_lim(self.zaxis, bottom, top, "bottom", "top",
720-
self.zz_viewLim, "intervalx", emit, auto, kw)
645+
return self._set_lim(self.zaxis, "z", bottom, top, "bottom", "top",
646+
self.zz_viewLim, "intervalx", emit, auto, **kw)
721647
set_zlim = set_zlim3d
722648

723649
def get_xlim3d(self):

0 commit comments

Comments
 (0)