Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 280acb8

Browse files
Commonize autoscaling each axis
1 parent 9b057ea commit 280acb8

1 file changed

Lines changed: 48 additions & 54 deletions

File tree

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,42 @@ def auto_scale_xyz(self, X, Y, Z=None, had_data=None):
632632
# Let autoscale_view figure out how to use this data.
633633
self.autoscale_view()
634634

635+
def _autoscale_axis(self, axis, v0, v1, minpos, margin, set_bound, _tight):
636+
"""
637+
Autoscale a single axis.
638+
639+
Parameters
640+
----------
641+
axis : Axis
642+
The axis to autoscale.
643+
v0, v1 : float
644+
Data interval limits.
645+
minpos : float
646+
Minimum positive value for log-scale handling.
647+
margin : float
648+
Margin to apply (e.g., self._xmargin).
649+
set_bound : callable
650+
Function to set the axis bound (e.g., self.set_xbound).
651+
_tight : bool
652+
Whether to use tight bounds.
653+
"""
654+
locator = axis.get_major_locator()
655+
v0, v1 = locator.nonsingular(v0, v1)
656+
# Validate limits for the scale (e.g., positive for log scale)
657+
v0, v1 = axis._scale.limit_range_for_scale(v0, v1, minpos)
658+
if margin > 0:
659+
# Apply margin in transformed space to handle non-linear scales
660+
transform = axis.get_transform()
661+
inverse_trans = transform.inverted()
662+
v0t, v1t = transform.transform([v0, v1])
663+
delta = (v1t - v0t) * margin
664+
if not np.isfinite(delta):
665+
delta = 0
666+
v0, v1 = inverse_trans.transform([v0t - delta, v1t + delta])
667+
if not _tight:
668+
v0, v1 = locator.view_limits(v0, v1)
669+
set_bound(v0, v1, self._view_margin)
670+
635671
def autoscale_view(self, tight=None,
636672
scalex=True, scaley=True, scalez=True):
637673
"""
@@ -656,64 +692,22 @@ def autoscale_view(self, tight=None,
656692
_tight = self._tight = bool(tight)
657693

658694
if scalex and self.get_autoscalex_on():
659-
x0, x1 = self.xy_dataLim.intervalx
660-
xlocator = self.xaxis.get_major_locator()
661-
x0, x1 = xlocator.nonsingular(x0, x1)
662-
# Validate limits for the scale (e.g., positive for log scale)
663-
minpos = self.xy_dataLim.minposx
664-
x0, x1 = self.xaxis._scale.limit_range_for_scale(x0, x1, minpos)
665-
if self._xmargin > 0:
666-
# Apply margin in transformed space to handle non-linear scales
667-
transform = self.xaxis.get_transform()
668-
inverse_trans = transform.inverted()
669-
x0t, x1t = transform.transform([x0, x1])
670-
delta = (x1t - x0t) * self._xmargin
671-
if not np.isfinite(delta):
672-
delta = 0
673-
x0, x1 = inverse_trans.transform([x0t - delta, x1t + delta])
674-
if not _tight:
675-
x0, x1 = xlocator.view_limits(x0, x1)
676-
self.set_xbound(x0, x1, self._view_margin)
695+
self._autoscale_axis(
696+
self.xaxis, *self.xy_dataLim.intervalx,
697+
self.xy_dataLim.minposx, self._xmargin,
698+
self.set_xbound, _tight)
677699

678700
if scaley and self.get_autoscaley_on():
679-
y0, y1 = self.xy_dataLim.intervaly
680-
ylocator = self.yaxis.get_major_locator()
681-
y0, y1 = ylocator.nonsingular(y0, y1)
682-
# Validate limits for the scale (e.g., positive for log scale)
683-
minpos = self.xy_dataLim.minposy
684-
y0, y1 = self.yaxis._scale.limit_range_for_scale(y0, y1, minpos)
685-
if self._ymargin > 0:
686-
# Apply margin in transformed space to handle non-linear scales
687-
transform = self.yaxis.get_transform()
688-
inverse_trans = transform.inverted()
689-
y0t, y1t = transform.transform([y0, y1])
690-
delta = (y1t - y0t) * self._ymargin
691-
if not np.isfinite(delta):
692-
delta = 0
693-
y0, y1 = inverse_trans.transform([y0t - delta, y1t + delta])
694-
if not _tight:
695-
y0, y1 = ylocator.view_limits(y0, y1)
696-
self.set_ybound(y0, y1, self._view_margin)
701+
self._autoscale_axis(
702+
self.yaxis, *self.xy_dataLim.intervaly,
703+
self.xy_dataLim.minposy, self._ymargin,
704+
self.set_ybound, _tight)
697705

698706
if scalez and self.get_autoscalez_on():
699-
z0, z1 = self.zz_dataLim.intervalx
700-
zlocator = self.zaxis.get_major_locator()
701-
z0, z1 = zlocator.nonsingular(z0, z1)
702-
# Validate limits for the scale (e.g., positive for log scale)
703-
minpos = self.zz_dataLim.minposx
704-
z0, z1 = self.zaxis._scale.limit_range_for_scale(z0, z1, minpos)
705-
if self._zmargin > 0:
706-
# Apply margin in transformed space to handle non-linear scales
707-
transform = self.zaxis.get_transform()
708-
inverse_trans = transform.inverted()
709-
z0t, z1t = transform.transform([z0, z1])
710-
delta = (z1t - z0t) * self._zmargin
711-
if not np.isfinite(delta):
712-
delta = 0
713-
z0, z1 = inverse_trans.transform([z0t - delta, z1t + delta])
714-
if not _tight:
715-
z0, z1 = zlocator.view_limits(z0, z1)
716-
self.set_zbound(z0, z1, self._view_margin)
707+
self._autoscale_axis(
708+
self.zaxis, *self.zz_dataLim.intervalx,
709+
self.zz_dataLim.minposx, self._zmargin,
710+
self.set_zbound, _tight)
717711

718712
def get_w_lims(self):
719713
"""Get 3D world limits."""

0 commit comments

Comments
 (0)