简体   繁体   中英

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.

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')

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM