8000 Merge pull request #22508 from anntzer/autoscaleaxis · matplotlib/matplotlib@cb8680f · GitHub
[go: up one dir, main page]

Skip to content

Commit cb8680f

Browse files
authored
Merge pull request #22508 from anntzer/autoscaleaxis
Move tracking of autoscale status to Axis.
2 parents 6d3a068 + cc88f31 commit cb8680f

File tree

4 files changed

+59
-85
lines changed

4 files changed

+59
-85
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,7 @@ def cla(self):
12241224
self.set_xlim(0, 1)
12251225
except TypeError:
12261226
pass
1227+
self.set_autoscalex_on(True)
12271228
if self._sharey is not None:
12281229
self.sharey(self._sharey)
12291230
else:
@@ -1232,17 +1233,14 @@ def cla(self):
12321233
self.set_ylim(0, 1)
12331234
except TypeError:
12341235
pass
1236+
self.set_autoscaley_on(True)
12351237

12361238
# update the minor locator for x and y axis based on rcParams
12371239
if mpl.rcParams['xtick.minor.visible']:
12381240
self.xaxis.set_minor_locator(mticker.AutoMinorLocator())
12391241
if mpl.rcParams['ytick.minor.visible']:
12401242
self.yaxis.set_minor_locator(mticker.AutoMinorLocator())
12411243

1242-
if self._sharex is None:
1243-
self._autoscaleXon = True
1244-
if self._sharey is None:
1245-
self._autoscaleYon = True
12461244
self._xmargin = mpl.rcParams['axes.xmargin']
12471245
self._ymargin = mpl.rcParams['axes.ymargin']
12481246
self._tight = None
@@ -2573,17 +2571,15 @@ def in_axes(self, mouseevent):
25732571
"""
25742572
return self.patch.contains(mouseevent)[0]
25752573

2574+
get_autoscalex_on = _axis_method_wrapper("xaxis", "_get_autoscale_on")
2575+
get_autoscaley_on = _axis_method_wrapper("yaxis", "_get_autoscale_on")
2576+
set_autoscalex_on = _axis_method_wrapper("xaxis", "_set_autoscale_on")
2577+
set_autoscaley_on = _axis_method_wrapper("yaxis", "_set_autoscale_on")
2578+
25762579
def get_autoscale_on(self):
25772580
"""Return True if each axis is autoscaled, False otherwise."""
2578-
return self._autoscaleXon and self._autoscaleYon
2579-
2580-
def get_autoscalex_on(self):
2581-
"""Return whether the x-axis is autoscaled."""
2582-
return self._autoscaleXon
2583-
2584-
def get_autoscaley_on(self):
2585-
"""Return whether the y-axis is autoscaled."""
2586-
return self._autoscaleYon
2581+
return all(axis._get_autoscale_on()
2582+
for axis in self._get_axis_map().values())
25872583

25882584
def set_autoscale_on(self, b):
25892585
"""
@@ -2594,30 +2590,8 @@ def set_autoscale_on(self, b):
25942590
----------
25952591
b : bool
25962592
"""
2597-
self._autoscaleXon A3DB = b
2598-
self._autoscaleYon = b
2599-
2600-
def set_autoscalex_on(self, b):
2601-
"""
2602-
Set whether the x-axis is autoscaled on the next draw or call to
2603-
`.Axes.autoscale_view`.
2604-
2605-
Parameters
2606-
----------
2607-
b : bool
2608-
"""
2609-
self._autoscaleXon = b
2610-
2611-
def set_autoscaley_on(self, b):
2612-
"""
2613-
Set whether the y-axis is autoscaled on the next draw or call to
2614-
`.Axes.autoscale_view`.
2615-
2616-
Parameters
2617-
----------
2618-
b : bool
2619-
"""
2620-
self._autoscaleYon = b
2593+
for axis in self._get_axis_map().values():
2594+
axis._set_autoscale_on(b)
26212595

26222596
@property
26232597
def use_sticky_edges(self):
@@ -2810,14 +2784,16 @@ def autoscale(self, enable=True, axis='both', tight=None):
28102784
scalex = True
28112785
scaley = True
28122786
else:
2813-
scalex = False
2814-
scaley = False
28152787
if axis in ['x', 'both']:
2816-
self._autoscaleXon = bool(enable)
2817-
scalex = self._autoscaleXon
2788+
self.set_autoscalex_on(bool(enable))
2789+
scalex = self.get_autoscalex_on()
2790+
else:
2791+
scalex = False
28182792
if axis in ['y', 'both']:
2819-
self._autoscaleYon = bool(enable)
2820-
scaley = self._autoscaleYon
2793+
self.set_autoscaley_on(bool(enable))
2794+
scaley = self.get_autoscaley_on()
2795+
else:
2796+
scaley = False
28212797
if tight and scalex:
28222798
self._xmargin = 0
28232799
if tight and scaley:
@@ -2876,13 +2852,13 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
28762852
# called very early in the Axes init process (e.g., for twin Axes)
28772853
# when these attributes don't even exist yet, in which case
28782854
# `get_children` would raise an AttributeError.
2879-
if self._xmargin and scalex and self._autoscaleXon:
2855+
if self._xmargin and scalex and self.get_autoscalex_on():
28802856
x_stickies = np.sort(np.concatenate([
28812857
artist.sticky_edges.x
28822858
for ax in self._shared_axes["x"].get_siblings(self)
28832859
if hasattr(ax, "_children")
28842860
for artist in ax.get_children()]))
2885-
if self._ymargin and scaley and self._autoscaleYon:
2861+
if self._ymargin and scaley and self.get_autoscaley_on():
28862862
y_stickies = np.sort(np.concatenate([
28872863
artist.sticky_edges.y
28882864
for ax in self._shared_axes["y"].get_siblings(self)
@@ -2893,10 +2869,10 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
28932869
if self.get_yscale() == 'log':
28942870
y_stickies = y_stickies[y_stickies > 0]
28952871

2896-
def handle_single_axis(scale, autoscaleon, shared_axes, name,
2897-
axis, margin, stickies, set_bound):
2872+
def handle_single_axis(
2873+
scale, shared_axes, name, axis, margin, stickies, set_bound):
28982874

2899-
if not (scale and autoscaleon):
2875+
if not (scale and axis._get_autoscale_on()):
29002876
return # nothing to do...
29012877

29022878
shared = shared_axes.get_siblings(self)
@@ -2959,11 +2935,11 @@ def handle_single_axis(scale, autoscaleon, shared_axes, name,
29592935
# End of definition of internal function 'handle_single_axis'.
29602936

29612937
handle_single_axis(
2962-
scalex, self._autoscaleXon, self._shared_axes["x"], 'x',
2963-
self.xaxis, self._xmargin, x_stickies, self.set_xbound)
2938+
scalex, self._shared_axes["x"], 'x', self.xaxis, self._xmargin,
2939+
x_stickies, self.set_xbound)
29642940
handle_single_axis(
2965-
scaley, self._autoscaleYon, self._shared_axes["y"], 'y',
2966-
self.yaxis, self._ymargin, y_stickies, self.set_ybound)
2941+
scaley, self._shared_axes["y"], 'y', self.yaxis, self._ymargin,
2942+
y_stickies, self.set_ybound)
29672943

29682944
def _update_title_position(self, renderer):
29692945
"""

lib/matplotlib/axis.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ def __init__(self, axes, pickradius=15):
685685

686686
self.clear()
687687
self._set_scale('linear')
688+
self._autoscale_on = True
688689

689690
@property
690691
def isDefault_majloc(self):
@@ -777,6 +778,21 @@ def _set_scale(self, value, **kwargs):
777778
def limit_range_for_scale(self, vmin, vmax):
778779
return self._scale.limit_range_for_scale(vmin, vmax, self.get_minpos())
779780

781+
def _get_autoscale_on(self):
782+
"""Return whether this Axis is autoscaled."""
783+
return self._autoscale_on
784+
785+
def _set_autoscale_on(self, b):
786+
"""
787+
Set whether this Axis is autoscaled when drawing or by
788+
`.Axes.autoscale_view`.
789+
790+
Parameters
791+
----------
792+
b : bool
793+
"""
794+
self._autoscale_on = b
795+
780796
def get_children(self):
781797
return [self.label, self.offsetText,
782798
*self.get_major_ticks(), *self.get_minor_ticks()]
@@ -1087,7 +1103,7 @@ def _set_lim(self, v0, v1, *, emit=True, auto):
10871103
for ax in self.axes._shared_axes[name].get_siblings(self.axes):
10881104
ax._stale_viewlims[name] = False
10891105
if auto is not None:
1090-
setattr(self.axes, f"_autoscale{name.upper()}on", bool(auto))
1106+
self._set_autoscale_on(bool(auto))
10911107

10921108
if emit:
10931109
self.axes.callbacks.process(f"{name}lim_changed", self.axes)

lib/matplotlib/image.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,9 +974,9 @@ def set_extent(self, extent):
974974
self.axes.update_datalim(corners)
975975
self.sticky_edges.x[:] = [xmin, xmax]
976976
self.sticky_edges.y[:] = [ymin, ymax]
977-
if self.axes._autoscaleXon:
977+
if self.axes.get_autoscalex_on():
978978
self.axes.set_xlim((xmin, xmax), auto=None)
979-
if self.axes._autoscaleYon:
979+
if self.axes.get_autoscaley_on():
980980
self.axes.set_ylim((ymin, ymax), auto=None)
981981
self.stale = True
982982

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -460,29 +460,8 @@ def get_axis_position(self):
460460
def update_datalim(self, xys, **kwargs):
461461
pass
462462

463-
def get_autoscale_on(self):
464-
# docstring inherited
465-
return super().get_autoscale_on() and self.get_autoscalez_on()
466-
467-
def get_autoscalez_on(self):
468-
"""Return whether the z-axis is autoscaled."""
469-
return self._autoscaleZon
470-
471-
def set_autoscale_on(self, b):
472-
# docstring inherited
473-
super().set_autoscale_on(b)
474-
self.set_autoscalez_on(b)
475-
476-
def set_autoscalez_on(self, b):
477-
"""
478-
Set whether the z-axis is autoscaled on the next draw or call to
479-
`.Axes.autoscale_view`.
480-
481-
Parameters
482-
----------
483-
b : bool
484-
"""
485-
self._autoscaleZon = b
463+
get_autoscalez_on = _axis_method_wrapper("zaxis", "_get_autoscale_on")
464+
set_autoscalez_on = _axis_method_wrapper("zaxis", "_set_autoscale_on")
486465

487466
def set_zmargin(self, m):
488467
"""
@@ -556,15 +535,18 @@ def autoscale(self, enable=True, axis='both', tight=None):
556535
scalez = True
557536
else:
558537
if axis in ['x', 'both']:
559-
self._autoscaleXon = scalex = bool(enable)
538+
self.set_autoscalex_on(bool(enable))
539+
scalex = self.get_autoscalex_on()
560540
else:
561541
scalex = False
562542
if axis in ['y', 'both']:
563-
self._autoscaleYon = scaley = bool(enable 10000 )
543+
self.set_autoscaley_on(bool(enable))
544+
scaley = self.get_autoscaley_on()
564545
else:
565546
scaley = False
566547
if axis in ['z', 'both']:
567-
self._autoscaleZon = scalez = bool(enable)
548+
self.set_autoscalez_on(bool(enable))
549+
scalez = self.get_autoscalez_on()
568550
else:
569551
scalez = False
570552
if scalex:
@@ -611,7 +593,7 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True,
611593
else:
612594
_tight = self._tight = bool(tight)
613595

614-
if scalex and self._autoscaleXon:
596+
if scalex and self.get_autoscalex_on():
615597
self._shared_axes["x"].clean()
616598
x0, x1 = self.xy_dataLim.intervalx
617599
xlocator = self.xaxis.get_major_locator()
@@ -624,7 +606,7 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True,
624606
x0, x1 = xlocator.view_limits(x0, x1)
625607
self.set_xbound(x0, x1)
626608

627-
if scaley and self._autoscaleYon:
609+
if scaley and self.get_autoscaley_on():
628610
self._shared_axes["y"].clean()
629611
y0, y1 = self.xy_dataLim.intervaly
630612
ylocator = self.yaxis.get_major_locator()
@@ -637,7 +619,7 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True,
637619
y0, y1 = ylocator.view_limits(y0, y1)
638620
self.set_ybound(y0, y1)
639621

640-
if scalez and self._autoscaleZon:
622+
if scalez and self.get_autoscalez_on():
641623
self._shared_axes["z"].clean()
642624
z0, z1 = self.zz_dataLim.interv 8064 alx
643625
zlocator = self.zaxis.get_major_locator()
@@ -974,7 +956,7 @@ def cla(self):
974956
except TypeError:
975957
pass
976958

977-
self._autoscaleZon = True
959+
self.set_autoscalez_on(True)
978960
if self._focal_length == np.inf:
979961
self._zmargin = rcParams['axes.zmargin']
980962
else:

0 commit comments

Comments
 (0)
0