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.