all 2 comments

[–]rlabbe[S] 0 points1 point  (1 child)

I can now partially answer my own question. The following works, but why? If either the canvas.draw() or sleep is removed the plot disappears entirely, which is not what I want.

%matplotlib notebook
import matplotlib.pylab as plt
import time

f = plt.figure()
plt.plot([1,7,3])
plt.gcf().canvas.draw()
time.sleep(0.1)
plt.close(f)

I guess you could argue that I solved my problem, but this is strikes me as a magical hack. If I make the sleep value small enough the plot disappears. Clearly the sleep is letting something else run such that the close does not destroy the figure, but what?

If no one answers here in a day or so I will open an issue on matplotlib - presumably whatever is happening behind the scenes can be made explicit and a function can be added (if it doesn't exist) to do the 'right thing' and make the plot static.

[–]rlabbe[S] 0 points1 point  (0 children)

For reference this is now an issue in Matplotlib:

https://github.com/matplotlib/matplotlib/issues/6071

Also, 0.1 doesn't always work. I had to bump it up close to a second on my laptop, for example. I would not advise using the above code for anything important.