10000 add context manager functionality to ion and ioff · matplotlib/matplotlib@e1aa03e · GitHub
[go: up one dir, main page]

Skip to content

Commit e1aa03e

Browse files
committed
add context manager functionality to ion and ioff
this enables with plt.ioff: or with plt.ion:
1 parent fbbc84f commit e1aa03e

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

lib/matplotlib/pyplot.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,16 +333,41 @@ def isinteractive():
333333
return matplotlib.is_interactive()
334334

335335

336-
def ioff():
337-
"""Turn the interactive mode off."""
338-
matplotlib.interactive(False)
339-
uninstall_repl_displayhook()
336+
class _ion_class():
337+
def __call__(self):
338+
"""Turn the interactive mode on."""
339+
matplotlib.interactive(True)
340+
install_repl_displayhook()
340341

342+
def __enter__(self):
343+
self.wasinteractive = isinteractive()
344+
self.__call__()
345+
346+
def __exit__(self, exc_type, exc_value, traceback):
347+
if not self.wasinteractive:
348+
matplotlib.interactive(False)
349+
uninstall_repl_displayhook()
350+
del self.wasinteractive
351+
352+
353+
class _ioff_class():
354+
def __call__(self):
355+
"""Turn the interactive mode off."""
356+
matplotlib.interactive(False)
357+
uninstall_repl_displayhook()
358+
359+
def __enter__(self):
360+
self.wasinteractive = isinteractive()
361+
self.__call__()
362+
363+
def __exit__(self, exc_type, exc_value, traceback):
364+
if self.wasinteractive:
365+
matplotlib.interactive(True)
366+
install_repl_displayhook()
367+
del self.wasinteractive
341368

342-
def ion():
343-
"""Turn the interactive mode on."""
344-
matplotlib.interactive(True)
345-
install_repl_displayhook()
369+
ioff = _ioff_class()
370+
ion = _ion_class()
346371

347372

348373
def pause(interval):

0 commit comments

Comments
 (0)
0