Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Stop Interaction for specific Figure in Jupyter notebook

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.
like image 992
ipap Avatar asked Dec 24 '16 19:12

ipap


2 Answers

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')
like image 68
Mike Müller Avatar answered Oct 03 '22 01:10

Mike Müller


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)
like image 29
FJDU Avatar answered Oct 03 '22 01:10

FJDU