Skip to content

Commit 102a1d2

Browse files
FIX: remove warning when clearing axes with shared axis (matplotlib#32069)
Co-authored-by: Saumya Agrawal <145997182+saumyacoder1709@users.noreply.github.com>
1 parent c2604bd commit 102a1d2

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

lib/matplotlib/axes/_base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,13 @@ def __clear(self):
13541354
xaxis_visible = self.xaxis.get_visible()
13551355
yaxis_visible = self.yaxis.get_visible()
13561356

1357-
for axis in self._axis_map.values():
1357+
for name, axis in self._axis_map.items():
13581358
axis.clear() # Also resets the scale to linear.
1359+
# need to do any shared axis scales as well
1360+
for other in axis._get_shared_axes():
1361+
if other is self.axes:
1362+
continue
1363+
other._axis_map[name]._set_scale("linear")
13591364
for spine in self.spines.values():
13601365
spine._clear() # Use _clear to not clear Axis again
13611366

lib/matplotlib/tests/test_axes.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from matplotlib.projections.geo import HammerAxes
3838
from matplotlib.projections.polar import PolarAxes
3939
import matplotlib.pyplot as plt
40+
import matplotlib.scale as mscales
4041
import matplotlib.text as mtext
4142
import matplotlib.ticker as mticker
4243
import matplotlib.transforms as mtransforms
@@ -9308,6 +9309,24 @@ def test_shared_axes_clear(fig_test, fig_ref):
93089309
ax.plot(x, y)
93099310

93109311

9312+
def test_shared_axes_clear_scale(recwarn):
9313+
_, axs = plt.subplots(1, 2, sharey=True)
9314+
x = range(1, 10)
9315+
axs[0].loglog(x, x)
9316+
axs[1].loglog(x, x)
9317+
axs[0].clear()
9318+
9319+
assert len(recwarn) == 0
9320+
9321+
# the cleared axes has linear on both axis
9322+
for axis in axs[0]._axis_map.values():
9323+
assert isinstance(axis._scale, mscales.LinearScale)
9324+
9325+
# the linked axes becomes linear on the shared y-axis
9326+
assert isinstance(axs[1].xaxis._scale, mscales.LogScale)
9327+
assert isinstance(axs[1].yaxis._scale, mscales.LinearScale)
9328+
9329+
93119330
def test_shared_axes_retick():
93129331
fig, axs = plt.subplots(2, 2, sharex='all', sharey='all')
93139332

0 commit comments

Comments
 (0)