Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d478070

Browse files
committed
ensure final state is same as initial for ion/ioff contexts
1 parent 7746cdf commit d478070

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/matplotlib/pyplot.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ def __exit__(self, exc_type, exc_value, traceback):
385385
if self.wasinteractive:
386386
matplotlib.interactive(True)
387387
install_repl_displayhook()
388+
else:
389+
matplotlib.interactive(False)
390+
uninstall_repl_displayhook()
388391

389392

390393
class _IonContext:
@@ -408,6 +411,9 @@ def __exit__(self, exc_type, exc_value, traceback):
408411
if not self.wasinteractive:
409412
matplotlib.interactive(False)
410413
uninstall_repl_displayhook()
414+
else:
415+
matplotlib.interactive(True)
416+
install_repl_displayhook()
411417

412418

413419
def ioff():

lib/matplotlib/tests/test_pyplot.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,46 @@ def test_ion():
110110
assert mpl.is_interactive()
111111
assert mpl.is_interactive()
112112

113+
113114
def test_nested_ion_ioff():
115+
# initial state is interactive
114116
plt.ion()
117+
118+
# mixed ioff/ion
115119
with plt.ioff():
116120
assert not mpl.is_interactive()
117121
with plt.ion():
118122
assert mpl.is_interactive()
119123
assert not mpl.is_interactive()
120124
assert mpl.is_interactive()
121125

126+
# redundant contexts
122127
with plt.ioff():
123128
with plt.ioff():
124129
assert not mpl.is_interactive()
125130
assert mpl.is_interactive()
126131

132+
with plt.ion():
133+
plt.ioff()
134+
assert mpl.is_interactive()
135+
136+
# initial state is not interactive
127137
plt.ioff()
138+
139+
# mixed ioff/ion
140+
with plt.ion():
141+
assert mpl.is_interactive()
142+
with plt.ioff():
143+
assert not mpl.is_interactive()
144+
assert mpl.is_interactive()
145+
assert not mpl.is_interactive()
146+
147+
# redunant contexts
128148
with plt.ion():
129149
with plt.ion():
130150
assert mpl.is_interactive()
131151
assert not mpl.is_interactive()
152+
153+
with plt.ioff():
154+
plt.ion()
155+
assert not mpl.is_interactive()

0 commit comments

Comments
 (0)