I just had the same problem. I call a function that generates multiple plot windows. Each time I call the function the popped up plot windows accumulate in number. Trying matplotlib.pyplot.close('All')
at the begining of the function didn't solve the problem. I solved the problem by calling matplotlib.pyplot.close(figure)
, where figure is a plot figure instance (object).
I maintain a list of my plot figure objects. So, it is a good idea to maintain a list, and then call matplotlib.pyplot.close(figure)
for each instance of a figure object:
import matplotlib.pyplot as plot
Add plot instance objects (figure,axis) to a list:
fig, (ax1,ax2) = plt.subplots(nrows=2)
figAxisDict = {'figure': fig, 'axis1': ax1, 'axis2':ax2}
figAxisList.append(figAxisDict)
Call the function for figure closing, and clear the list afterwards:
if len(figAxisList) !=0:
for figAxis in figAxisList:
figure=figAxis['figure']
plot.close(figure)
figAxisList[:]=[]