Skip to content

add context manager capabilities to ioff when using ipympl #220

Closed
matplotlib/matplotlib
#17371
@ianhi

Description

@ianhi

Often when I'm using the widget backend in jupyterlab and want to do something like define a small layout of ipywidgets and plots I find myself using this pattern:

plt.ioff()
fig = plt.figure()
#... other things I don't want to be interactive
plt.ion()
....
widgets.VBox([slider1,slider2,fig.canvas])
  1. It took me quite a while to figure out that I needed to use ioff and ion to prevent multiple views of the figure showing up. It would be great if more discussion of this was included in the documentation (Documentation #208) and examples
  2. I like the idea of being able to use ioff as a context manager at least in the context of this backend. I propsed this on the matplotlib repo Request: provide a contextmanager for ioff or allow plt.figure(draw_on_create=False) matplotlib#17013 but that may not have been the right place for it as it would be useful primarily for this backend

Could this backend redefine iofflike so:

class ioff:
    def __call__(self):
        matplotlib.interactive(False)
        uninstall_repl_displayhook()
        
    def __enter__(self):
        self.call()
        
    def __exit__(self):
        ion()

I imagine this occuring in the ipympl __init__.py. This would enable the above example to be the slightly cleaner:

with plt.ioff:
   fig = plt.figure()
   #... other things I don't want to be interactive
widgets.VBox([slider1,slider2,fig.canvas])

Another issue where the usage of ioff/ion came up is: #203 though there was no discussion of the context manager there.

Currently I just always import pyplot as plt and just redefine plt.ioff to be the above class which works fairly well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions