Matplotlib runs out of memory when plotting in a loop

后端 未结 3 1835
北海茫月
北海茫月 2020-11-28 12:07

I have a fairly simple plotting routine that looks like this:

from __future__ import division
import datetime
import matplotlib
matplotlib.use(\'Agg\')
from          


        
3条回答
  •  孤城傲影
    2020-11-28 12:16

    Is each loop supposed to generate a new figure? I don't see you closing it or creating a new figure instance from loop to loop.

    This call will clear the current figure after you save it at the end of the loop:

    pyplot.clf()

    I'd refactor, though, and make your code more OO and create a new figure instance on each loop:

    from matplotlib import pyplot
    
    while True:
      fig = pyplot.figure()
      ax = fig.add_subplot(111)
      ax.plot(x,y)
      ax.legend(legendStrings, loc = 'best')
      fig.savefig('himom.png')
      # etc....
    

提交回复
热议问题