Pergunta

I have noticed that when I run:

import pylab as pl
pl.ion()
# Plot something
pl.show()
pl.close()

The last statement does not fully close the Figure. The figure goes dark, and the contents go away, but the Figure stays on the screen until I exit IPython as shown below

                      enter image description here

I am using the latest stable version of matplotlib (1.3.1) using an Anaconda distribution, on Linux 64 bit, and I connect remotely using ssh -X.

The backend I am using is below:

backend : QT4Agg
backend.qt4 : PySide
Foi útil?

Solução

you have to specify wich figure you want to close. In case you want to close all of them:

pl.close('all')

Also, there is a way to just clear but not close a figure:

pl.clf()

Also, seen below from another SO question:

Remember that plt.show() is a blocking function, so in the example code you used above, plt.close() isn't being executed until the window is closed, which makes it redundant.

You can use plt.ion() at the beginning of your code to make it non-blocking, although this has other implications.

Outras dicas

You can also use the following lines after your plotting

#Your Plotting function
plt.waitforbuttonpress(0) 
plt.close(fig)

The plt.waitforbuttonpress(0) will wait until a user input (Key press) is given. After that it will properly close the matplotlib window properly. It is very important to specify which figure to close.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top