Skip to content

Commit 716a35f

Browse files
committed
Let Axes.clear iterate over Axises.
... so that the implementation also works for 3D. This means also adding a sharez() method, which is copy-pasted from sharex()/sharey() (note that it is resolves a slight inconsistency in the old Axes3D.clear, which would previously not copy the z-autoscaleness status of a z-shared axes). Using Axis._set_lim in the loop also means that we need to pass auto explicitly to it anyways (it is a required parameter), so we can get rid of the set_autoscalex/y/z calls. Also, note that Axis.clear() explicitly resets the scale to "linear", so the extra `_set_scale("linear")` calls are unneeded. Finally, remove the silencing of TypeErrors when setting limits in clear(), which was added in 917de33 to handle initialization order problems in Axes3D but which seem not needed anymore (and are rather unsightly, as it's not immediately clear what we're really catching).
1 parent 446de7b commit 716a35f

File tree

3 files changed

+29
-40
lines changed

3 files changed

+29
-40
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,34 +1212,22 @@ def clear(self):
12121212
xaxis_visible = self.xaxis.get_visible()
12131213
yaxis_visible = self.yaxis.get_visible()
12141214

1215-
self.xaxis.clear()
1216-
self.yaxis.clear()
1217-
1218-
for name, spine in self.spines.items():
1215+
for axis in self._axis_map.values():
1216+
axis.clear() # Also resets the scale to linear.
1217+
for spine in self.spines.values():
12191218
spine.clear()
12201219

12211220
self.ignore_existing_data_limits = True
12221221
self.callbacks = cbook.CallbackRegistry(
12231222
signals=["xlim_changed", "ylim_changed", "zlim_changed"])
12241223

1225-
if self._sharex is not None:
1226-
self.sharex(self._sharex)
1227-
else:
1228-
self.xaxis._set_scale('linear')
1229-
try:
1230-
self.set_xlim(0, 1)
1231-
except TypeError:
1232-
pass
1233-
self.set_autoscalex_on(True)
1234-
if self._sharey is not None:
1235-
self.sharey(self._sharey)
1236-
else:
1237-
self.yaxis._set_scale('linear')
1238-
try:
1239-
self.set_ylim(0, 1)
1240-
except TypeError:
1241-
pass
1242-
self.set_autoscaley_on(True)
1224+
for name, axis in self._axis_map.items():
1225+
share = getattr(self, f"_share{name}")
1226+
if share is not None:
1227+
getattr(self, f"share{name}")(share)
1228+
else:
1229+
axis._set_scale("linear")
1230+
axis._set_lim(0, 1, auto=True)
12431231

12441232
# update the minor locator for x and y axis based on rcParams
12451233
if mpl.rcParams['xtick.minor.visible']:

lib/matplotlib/axis.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,6 @@ def __init__(self, axes, pickradius=15):
689689
self._minor_tick_kw = dict()
690690

691691
self.clear()
692-
self._set_scale('linear')
693692
self._autoscale_on = True
694693

695694
@property

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -933,30 +933,32 @@ def can_pan(self):
933933
"""
934934
return False
935935

936+
def sharez(self, other):
937+
"""
938+
Share the z-axis with *other*.
939+
940+
This is equivalent to passing ``sharex=other`` when constructing the
941+
Axes, and cannot be used if the z-axis is already being shared with
942+
another Axes.
943+
"""
944+
_api.check_isinstance(maxes._base._AxesBase, other=other)
945+
if self._sharez is not None and other is not self._sharez:
946+
raise ValueError("z-axis is already shared")
947+
self._shared_axes["z"].join(self, other)
948+
self._sharez = other
949+
self.zaxis.major = other.zaxis.major # Ticker instances holding
950+
self.zaxis.minor = other.zaxis.minor # locator and formatter.
951+
z0, z1 = other.get_zlim()
952+
self.set_zlim(z0, z1, emit=False, auto=other.get_autoscalez_on())
953+
self.zaxis._scale = other.zaxis._scale
954+
936955
def clear(self):
937956
# docstring inherited.
938957
super().clear()
939-
self.zaxis.clear()
940-
941-
if self._sharez is not None:
942-
self.zaxis.major = self._sharez.zaxis.major
943-
self.zaxis.minor = self._sharez.zaxis.minor
944-
z0, z1 = self._sharez.get_zlim()
945-
self.set_zlim(z0, z1, emit=False, auto=None)
946-
self.zaxis._set_scale(self._sharez.zaxis.get_scale())
947-
else:
948-
self.zaxis._set_scale('linear')
949-
try:
950-
self.set_zlim(0, 1)
951-
except TypeError:
952-
pass
953-
954-
self.set_autoscalez_on(True)
955958
if self._focal_length == np.inf:
956959
self._zmargin = rcParams['axes.zmargin']
957960
else:
958961
self._zmargin = 0.
959-
960962
self.grid(rcParams['axes3d.grid'])
961963

962964
def _button_press(self, event):

0 commit comments

Comments
 (0)