Skip to content

Commit 866c215

Browse files
authored
Merge pull request matplotlib#23501 from anntzer/clear
Let Axes.clear iterate over Axises.
2 parents d0478d1 + 716a35f commit 866c215

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)