8000 Move tracking of autoscale status to Axis. · matplotlib/matplotlib@cc88f31 · GitHub
[go: up one dir, main page]

Skip to content

Commit cc88f31

Browse files
committed
Move tracking of autoscale status to Axis.
As always, this can makes it easier to write generic code looping over all axises.
1 parent 53c5f88 commit cc88f31

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
@@ -1212,6 +1212,7 @@ def cla(self):
12121212
self.set_xlim(0, 1)
12131213
except TypeError:
12141214
pass
1215+
self.set_autoscalex_on(True)
12151216
if self._sharey is not None:
12161217
self.sharey(self._sharey)
12171218
else:
@@ -1220,17 +1221,14 @@ def cla(self):
12201221
self.set_ylim(0, 1)
12211222
except TypeError:
12221223
pass
1224+
self.set_autoscaley_on(True)
12231225

12241226
# update the minor locator for x and y axis based on rcParams
12251227
if mpl.rcParams['xtick.minor.visible']:
12261228
self.xaxis.set_minor_locator(mticker.AutoMinorLocator())
12271229
if mpl.rcParams['ytick.minor.visible']:
12281230
self.yaxis.set_minor_locator(mticker.AutoMinorLocator())
12291231

1230-
if self._sharex is None:
1231-
self._autoscaleXon = True
1232-
if self._sharey is None:
1233-
self._autoscale 8000 Yon = True
12341232
self._xmargin = mpl.rcParams['axes.xmargin']
12351233
self._ymargin = mpl.rcParams['axes.ymargin']
12361234
self._tight = None
@@ -2561,17 +2559,15 @@ def in_axes(self, mouseevent):
25612559
"""
25622560
return self.patch.contains(mouseevent)[0]
25632561

2562+
get_autoscalex_on = _axis_method_wrapper("xaxis", "_get_autoscale_on")
2563+
get_autoscaley_on = _axis_method_wrapper("yaxis", "_get_autoscale_on")
2564+
set_autoscalex_on = _axis_method_wrapper("xaxis", "_set_autoscale_on")
2565+
set_autoscaley_on = _axis_method_wrapper("yaxis", "_set_autoscale_on")
2566+
25642567
def get_autoscale_on(self):
25652568
"""Return True if each axis is autoscaled, False otherwise."""
2566-
return self._autoscaleXon and self._autoscaleYon
2567-
2568-
def get_autoscalex_on(self):
2569-
"""Return whether the x-axis is autoscaled."""
2570-
return self._autoscaleXon
2571-
2572-
def get_autoscaley_on(self):
2573-
"""Return whether the y-axis is autoscaled."""
2574-
return self._autoscaleYon
2569+
return all(axis._get_autoscale_on()
2570+
for axis in self._get_axis_map().values())
25752571

25762572
def set_autoscale_on(self, b):
25772573
"""
@@ -2582,30 +2578,8 @@ def set_autoscale_on(self, b):
25822578
----------
25832579
b : bool
25842580
"""
2585-
self._autoscaleXon = b
2586-
self._autoscaleYon = b
2587-
2588-
def set_autoscalex_on(self, b):
2589-
"""
2590-
Set whether the x-axis is autoscaled on the next draw or call to
2591-
`.Axes.autoscale_view`.
2592-
2593-
Parameters
2594-
----------
2595-
b : bool
2596-
"""
2597-
self._autoscaleXon = b
2598-
2599-
def set_autoscaley_on(self, b):
2600-
"""
2601-
Set whether the y-axis is autoscaled on the next draw or call to
2602-
`.Axes.autoscale_view`.
2603-
2604-
Parameters
2605-
----------
2606-
b : bool
2607-
"""
2608-
self._autoscaleYon = b
2581+
for axis in self._get_axis_map().values():
2582+
axis._set_autoscale_on(b)
26092583

26102584
@property
26112585
def use_sticky_edges(self):
@@ -2798,14 +2772,16 @@ def autoscale(self, enable=True, axis='both', tight=None):
27982772
scalex = True
27992773
scaley = True
28002774
else:
2801-
scalex = False
2802-
scaley = False
28032775
if axis in ['x', 'both']:
2804-
self._autoscaleXon = bool(enable)
2805-
scalex = self._autoscaleXon
2776+
self.set_autoscalex_on(bool(enable))
2777+
scalex = self.get_autoscalex_on()
2778+
else:
2779+
scalex = False
28062780
if axis in ['y', 'both']:
2807-
self._autoscaleYon = bool(enable)
2808-
scaley = self._autoscaleYon
2781+
self.set_autoscaley_on(bool(enable))
2782+
scaley = self.get_autoscaley_on()
2783+
else:
2784+
scaley = False
28092785
if tight and scalex:
28102786
self._xmargin = 0
28112787
if tight and scaley:
@@ -2864,13 +2840,13 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
28642840
# called very early in the Axes init process (e.g., for twin Axes)
28652841
# when these attributes don't even exist yet, in which case
28662842
# `get_children` would raise an AttributeError.
2867-
if self._xmargin and scalex and self._autoscaleXon:
2843+
if self._xmargin and scalex and self.get_autoscalex_on():
28682844
x_stickies = np.sort(np.concatenate([
28692845
artist.sticky_edges.x
28702846
for ax in self._shared_axes["x"].get_siblings(self)
28712847
if hasattr(ax, "_children")
28722848
for artist in ax.get_children()]))
2873-
if self._ymargin and scaley and self._autoscaleYon:
2849+
if self._ymargin and scaley and self.get_autoscaley_on():
28742850
y_stickies = np.sort(np.concatenate([
28752851
artist.sticky_edges.y
28762852
for ax in self._shared_axes["y"].get_siblings(self)
@@ -2881,10 +2857,10 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
28812857
if self.get_yscale() == 'log':
28822858
y_stickies = y_stickies[y_stickies > 0]
28832859

2884-
def handle_single_axis(scale, autoscaleon, shared_axes, name,
2885-
axis, margin, stickies, set_bound):
2860+
def handle_single_axis(
2861+
scale, shared_axes, name, axis, margin, stickies, set_bound):
28862862

2887-
if not (scale and autoscaleon):
2863+
if not (scale and axis._get_autoscale_on()):
28882864
return # nothing to do...
28892865

28902866
shared = shared_axes.get_siblings(self)
@@ -2947,11 +2923,11 @@ def handle_single_axis(scale, autoscaleon, shared_axes, name,
29472923
# End of definition of internal function 'handle_single_axis'.
29482924

29492925
handle_single_axis(
2950-
scalex, self._autoscaleXon, self._shared_axes["x"], 'x',
2951-
self.xaxis, self._xmargin, x_stickies, self.set_xbound)
2926+
scalex, self._shared_axes["x"], 'x', self.xaxis, self._xmargin,
2927+
x_stickies, self.set_xbound)
29522928
handle_single_axis(
2953-
scaley, self._autoscaleYon, self._shared_axes["y"], 'y',
2954-
self.yaxis, self._ymargin, y_stickies, self.set_ybound)
2929+
scaley, self._shared_axes["y"], 'y', self.yaxis, self._ymargin,
2930+
y_stickies, self.set_ybound)
29552931

29562932
def _get_axis_list(self):
29572933
return tuple(getattr(self, f"{name}axis") for name in self._axis_names)

lib/matplotlib/axis.py

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

687687
self.clear()
688688
self._set_scale('linear')
689+
self._autoscale_on = True
689690

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

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

10931109
if emit:
10941110
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
@@ -976,9 +976,9 @@ def set_extent(self, extent):
976976
self.axes.update_datalim(corners)
977977
self.sticky_edges.x[:] = [xmin, xmax]
978978
self.sticky_edges.y[:] = [ymin, ymax]
979-
if self.axes._autoscaleXon:
979+
if self.axes.get_autoscalex_on():
980980
self.axes.set_xlim((xmin, xmax), auto=None)
981-
if self.axes._autoscaleYon:
981+
if self.axes.get_autoscaley_on():
982982
self.axes.set_ylim((ymin, ymax), auto=None)
983983
self.stale = True
984984

lib/mpl_toolkits/mplot3d/axes3d.py

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

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

489468
def set_zmargin(self, m):
490469
"""
@@ -558,15 +537,18 @@ def autoscale(self, enable=True, axis='both', tight=None):
558537
scalez = True
559538
else:
560539
if axis in ['x', 'both']:
561-
self._autoscaleXon = scalex = bool(enable)
540+
self.set_autoscalex_on(bool(enable))
541+
scalex = self.get_autoscalex_on()
562542
else:
563543
scalex = False
564544
if axis in ['y', 'both']:
565-
self._autoscaleYon = scaley = bool(enable)
545+
self.set_autoscaley_on(bool(enable))
546+
scaley = self.get_autoscaley_on()
566547
else:
567548
scaley = False
568549
if axis in ['z', 'both']:
569-
self._autoscaleZon = scalez = bool(enable)
550+
self.set_autoscalez_on(bool(enable))
551+
scalez = self.get_autoscalez_on()
570552
else:
571553
scalez = False
572554
if scalex:
@@ -613,7 +595,7 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True,
613595
else:
614596
_tight = self._tight = bool(tight)
615597

616-
if scalex and self._autoscaleXon:
598+
if scalex and self.get_autoscalex_on():
617599
self._shared_axes["x"].clean()
618600
x0, x1 = self.xy_dataLim.intervalx
619601
xlocator = self.xaxis.get_major_locator()
@@ -626,7 +608,7 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True,
626608
x0, x1 = xlocator.view_limits(x0, x1)
627609
self.set_xbound(x0, x1)
628610

629-
if scaley and self._autoscaleYon:
611+
if scaley and self.get_autoscaley_on():
630612
self._shared_axes["y"].clean()
631613
y0, y1 = self.xy_dataLim.intervaly
632614
ylocator = self.yaxis.get_major_locator()
@@ -639,7 +621,7 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True,
639621
y0, y1 = ylocator.view_limits(y0, y1)
640622
self.set_ybound(y0, y1)
641623

642-
if scalez and self._autoscaleZon:
624+
if scalez and self.get_autoscalez_on():
643625
self._shared_axes["z"].clean()
644626
z0, z1 = self.zz_dataLim.intervalx
645627
zlocator = self.zaxis.get_major_locator()
@@ -976,7 +958,7 @@ def cla(self):
976958
except TypeError:
977959
pass
978960

979-
self._autoscaleZon = True
961+
self.set_autoscalez_on(True)
980962
if self._focal_length == np.inf:
981963
self._zmargin = rcParams['axes.zmargin']
982964
else:

0 commit comments

Comments
 (0)
0