编辑:正如MattiPastell的解决方案所示,还有一个更好的方法:使用plt.get_fignums()
。
import numpy as npimport pylabimport matplotlib._pylab_helpersx=np.random.random((10,10))y=np.random.random((10,10))pylab.figure()pylab.plot(x)pylab.figure()pylab.plot(y)figures=[manager.canvas.figure for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]print(figures)# [<matplotlib.figure.Figure object at 0xb788ac6c>, <matplotlib.figure.Figure object at 0xa143d0c>]for i, figure in enumerate(figures): figure.savefig('figure%d.png' % i)