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

Skip to content

Commit 8c3e60c

Browse files
committed
add context manager functionality to ion and ioff
this enables usage of plt.ioff as a context manager to allow creating figures without the figure automatically displaying in environments such as jupyter. Usage: with plt.ioff: plt.figure()
1 parent 33503a2 commit 8c3e60c

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

lib/matplotlib/pyplot.py

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -364,36 +364,62 @@ def isinteractive():
364364
return matplotlib.is_interactive()
365365

366366

367-
def ioff():
368-
"""
369-
Turn the interactive mode off.
367+
class _ioff_class():
368+
def __call__(self):
369+
"""
370+
Turn the interactive mode off.
371+
372+
See Also
373+
--------
374+
ion : enable interactive mode
375+
isinteractive : query current state
376+
377+
show : show windows (and maybe block)
378+
pause : show windows, run GUI event loop, and block for a time
379+
"""
380+
matplotlib.interactive(False)
381+
uninstall_repl_displayhook()
370382

371-
See Also
372-
--------
373-
ion : enable interactive mode
374-
isinteractive : query current state
383+
def __enter__(self):
384+
self.wasinteractive = isinteractive()
385+
self.__call__()
375386

376-
show : show windows (and maybe block)
377-
pause : show windows, run GUI event loop, and block for a time
378-
"""
379-
matplotlib.interactive(False)
380-
uninstall_repl_displayhook()
387+
def __exit__(self, exc_type, exc_value, traceback):
388+
if self.wasinteractive:
389+
matplotlib.interactive(True)
390+
install_repl_displayhook()
391+
del self.wasinteractive
381392

382393

383-
def ion():
384-
"""
385-
Turn the interactive mode on.
394+
class _ion_class():
395+
def __call__(self):
396+
"""
397+
Turn the interactive mode on.
386398
387-
See Also
388-
--------
389-
ioff : disable interactive mode
390-
isinteractive : query current state
399+
See Also
400+
--------
401+
ioff : disable interactive mode
402+
isinteractive : query current state
391403
392-
show : show windows (and maybe block)
393-
pause : show windows, run GUI event loop, and block for a time
394-
"""
395-
matplotlib.interactive(True)
396-
install_repl_displayhook()
404+
show : show windows (and maybe block)
405+
pause : show windows, run GUI event loop, and block for a time
406+
"""
407+
matplotlib.interactive(True)
408+
install_repl_displayhook()
409+
410+
def __enter__(self):
411+
self.wasinteractive = isinteractive()
412+
self.__call__()
413+
414+
def __exit__(self, exc_type, exc_value, traceback):
415+
if not self.wasinteractive:
416+
matplotlib.interactive(False)
417+
uninstall_repl_displayhook()
418+
del self.wasinteractive
419+
420+
421+
ioff = _ioff_class()
422+
ion = _ion_class()
397423

398424

399425
def pause(interval):

0 commit comments

Comments
 (0)
0