Reputation: 515
Many plots just doesn't need to be interactive so I tried to change them to inline plots.
I tried the following without success:
plt.close(fig)
. It clear the figure
plt.ioff()
, failed
wrap codes between %matplotlib inline
%matplotlib notebook
. It close other interactive plots
Upvotes: 2
Views: 569
Reputation: 339052
There can only ever be one single backend be active. It would be possible to change the backend, but that would require to close the interactive figures.
An option is to work with interactive backend throughout (e.g. %matplotlib widget
) and call a custom function that shows a png image inline once that is desired.
#Cell1
%matplotlib widget
#Cell2
import matplotlib.pyplot as plt
def fig2inline(fig):
from IPython.display import display, Image
from io import BytesIO
plt.close(fig)
buff = BytesIO()
fig.savefig(buff, format='png')
buff.seek(0)
display(Image(data=buff.getvalue()))
#Cell3: (show the interactive plot)
fig, ax = plt.subplots(figsize=(3, 1.7))
ax.plot([1,3,4]);
#Cell4: (show the inline plot)
fig2, ax2 = plt.subplots(figsize=(3, 1.7))
ax2.plot([3,1,1]);
fig2inline(fig2)
Upvotes: 2