Matplotlib和Pyplot.close()没有释放内存? - 后端相关的Qt4Agg

Fak*_*DIY 39 python memory backend matplotlib

编辑:如果我明确地将matplotlib的后端从'Qt4Agg'更改为'Agg',那么我能够运行我的代码而没有错误.我认为这是后端的一个错误?

我正在编写一些代码来自动处理相当大量的数据.代码首先解析我的数据文件并存储所有相关的位.然后,我有不同的功能来生成我需要的每个图形(总共大约有25个).但是,我一直遇到某种内存错误,我认为这是因为Matplotlib/PyPlot没有正确释放内存.

每个绘图函数都以pyplot.close(fig)命令结束,因为我只想保存图形而不立即查看它们,所以它们包含pyplot.show().

如果我在翻译中单独运行绘图功能,那么我不会遇到任何问题.但是,如果我创建一个单独调用每个绘图函数的函数,那么我会遇到"MemoryError:无法为路径分配内存".

有人遇到过这样的问题吗?在绘制循环时,似乎与Matplotlib内存不足有关,但pyplot.close()无法解决我的问题.

这是我的代码中典型的绘图函数:

def TypicalPlot(self, title=None, comment=False, save=False, show=True):

    if title is None:
        title = self.dat.title

    fig = plt.figure()
    host = SubplotHost(fig, 111)
    fig.add_subplot(host)
    par = host.twinx()
    host.set_xlabel("Time (hrs)")
    host.set_ylabel("Power (W)")
    par.set_ylabel("Temperature (C)")
    p1, = host.plot(self.dat.timebase1, self.dat.pwr, 'b,', label="Power",
                    markevery= self.skip)
    p2, = par.plot(self.dat.timebase2, self.dat.Temp1, 'r,', 
                   label="Temp 1", markevery= self.skip)
    p3, = par.plot(self.dat.timebase2, self.dat.Temp2, 'g,', 
                   label="Temp 2", markevery= self.skip)
    p4, = par.plot(self.dat.timebase2, self.dat.Temp3, 'm,', 
                   label="Temp 3", markevery= self.skip)
    host.axis["left"].label.set_color(p1.get_color())
    # par.axis["right"].label.set_color(p2.get_color())
    #host.legend(loc='lower left')
    plt.title(title+" Temperature")

    leg=host.legend(loc='lower left',fancybox=True)
    #leg.get_frame().set_alpha(0.5)
    frame  = leg.get_frame()
    frame.set_facecolor('0.80')

    ### make the legend text smaller
    for t in leg.get_texts():
        t.set_fontsize('small')

    ### set the legend text color to the same color as the plots for added
    ### readability
    leg.get_texts()[0].set_color(p1.get_color())
    leg.get_texts()[1].set_color(p2.get_color())
    leg.get_texts()[2].set_color(p3.get_color())    
    leg.get_texts()[3].set_color(p4.get_color())        

    if show is True and save is True:
        plt.show()
        plt.savefig('temp.png')
    elif show is True and save is False:
        plt.show()
    elif show is False and save is True:
        plt.savefig('temp.png')
        plt.clf()
        plt.close(fig)
Run Code Online (Sandbox Code Playgroud)

如果我现在在终端跑

MyClass.TypicalPlot(save=True, show = False) 
Run Code Online (Sandbox Code Playgroud)

然后我没有得到任何错误.我的所有绘图功能都是如此.

如果我创建一个新函数来执行此操作:

def saveAllPlots(self, comments = False):

        if self.comment is None: comment = False
        else: comment = True
        self.TypicalPlot(save=True, show=False, comment=comment)
        self.AnotherPlot(save=True, show=False)
        self.AnotherPlot2(save=True, show=False)
        self.AnotherPlot3(save=True, show=False)
        ...etc, etc, etc
Run Code Online (Sandbox Code Playgroud)

然后它运行大约一半的图形,然后我得到"MemoryError:无法为路径分配内存".

Rul*_*rld 1

我认为它这样做的原因是因为当它遍历所有不同的图表时,它会耗尽内存,可能是因为它没有正确释放它。

为什么不尝试创建大约 3 个左右的程序,每个程序都执行一些图表,而不是一个程序执行所有图表:

方案1:图1-8

方案2:图9-16

程序 3:图 17-25

希望这有帮助@FakeDIY :)