13

In case there is no need for interaction for a specific matplotlib figure in a Jupyter notebook, how to prevent that programmatically?

Manually that can be done by pressing Ctrl-w or clicking the "Stop Interaction" button. I am looking for the API access to the same operation.

Reasons:

  • Interactive figures use resources and warnings displayed for too many such figures.
  • Closing them manually is not convenient each time the cells are executed
  • Without interactive frame figures are more compact.
Cœur
  • 37,241
  • 25
  • 195
  • 267
ipap
  • 301
  • 2
  • 11

3 Answers3

7

You can switch between notebook mode with interactivity and inline mode without such interactivity with:

%matplotlib inline

and

%matplotlib notebook

You can do this programmatically in the notebook with:

get_ipython().magic('matplotlib notebook')

or:

get_ipython().magic('matplotlib inline')
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • No, unfortunately `get_ipython().magic('matplotlib inline')` renders ALL figures in the notebook as inline, not only the next one. – ipap Dec 30 '16 at 21:50
  • You need to call `get_ipython().magic('matplotlib notebook')` to switch back to Notebook mode. So just toggle between both modes with the help of the two lines from my answer. – Mike Müller Dec 30 '16 at 22:02
  • 1
    I understood that. But after switching to inline ALL the figures in the notebook which were in the interactive mode by than become `inline` and loose their interactivity, while I need that only for one specific figure. – ipap Dec 31 '16 at 23:47
1

The following seems to work, though this is not ideal.

In cell 1

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot([0,1])

In cell 2

plt.close(fig)
FJDU
  • 1,425
  • 2
  • 15
  • 21
0

You can stop interaction using : plt.ioff()

at the end of the plot.

msabr
  • 1