Skip to content

Commit b4563d8

Browse files
committed
BUG : fix cla behavior with twinx
Closes #3633 Added more special-case logic for twined axes.
1 parent 68b34b0 commit b4563d8

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,16 @@ def _gen_axes_spines(self, locations=None, offset=0.0, units='inches'):
816816
def cla(self):
817817
"""Clear the current axes."""
818818
# Note: this is called by Axes.__init__()
819+
820+
# stash the current visibility state
821+
if hasattr(self, 'patch'):
822+
patch_visible = self.patch.get_visible()
823+
else:
824+
patch_visible = True
825+
826+
xaxis_visible = self.xaxis.get_visible()
827+
yaxis_visible = self.yaxis.get_visible()
828+
819829
self.xaxis.cla()
820830
self.yaxis.cla()
821831
for name, spine in six.iteritems(self.spines):
@@ -941,6 +951,13 @@ def cla(self):
941951

942952
self._shared_x_axes.clean()
943953
self._shared_y_axes.clean()
954+
if self._sharex:
955+
self.xaxis.set_visible(xaxis_visible)
956+
self.patch.set_visible(patch_visible)
957+
958+
if self._sharey:
959+
self.yaxis.set_visible(yaxis_visible)
960+
self.patch.set_visible(patch_visible)
944961

945962
def clear(self):
946963
"""clear the axes"""

lib/matplotlib/tests/test_axes.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import six
55
from six.moves import xrange
66

7-
from nose.tools import assert_equal, assert_raises
7+
from nose.tools import assert_equal, assert_raises, assert_false, assert_true
88
import datetime
99

1010
import numpy as np
@@ -100,6 +100,30 @@ def test_twin_axis_locaters_formatters():
100100
ax3 = ax1.twinx()
101101

102102

103+
@cleanup
104+
def test_twinx_cla():
105+
fig, ax = plt.subplots()
106+
ax2 = ax.twinx()
107+
ax3 = ax2.twiny()
108+
plt.draw()
109+
assert_false(ax2.xaxis.get_visible())
110+
assert_false(ax2.patch.get_visible())
111+
ax2.cla()
112+
ax3.cla()
113+
114+
assert_false(ax2.xaxis.get_visible())
115+
assert_false(ax2.patch.get_visible())
116+
assert_true(ax2.yaxis.get_visible())
117+
118+
assert_true(ax3.xaxis.get_visible())
119+
assert_false(ax3.patch.get_visible())
120+
assert_false(ax3.yaxis.get_visible())
121+
122+
assert_true(ax.xaxis.get_visible())
123+
assert_true(ax.patch.get_visible())
124+
assert_true(ax.yaxis.get_visible())
125+
126+
103127
@image_comparison(baseline_images=["autoscale_tiny_range"], remove_text=True)
104128
def test_autoscale_tiny_range():
105129
# github pull #904

0 commit comments

Comments
 (0)