首页 文章

清理matplotlib的队列,并保存大的pdf数字

提问于
浏览
0

我需要使用来自不同文件的数据在同一个pdf文件中保存不同的图 . 我使用这个代码,每个步骤打开某个文件使用一些函数分析它并保存在pdf文件中 . 我的问题是matplotlib的队列增加了,最后如果我说plt.show()我得到了大量不同的数字 . 我不想要这个,我想在不看情节的情况下清理maplotlib的队列 .

我使用的代码是:

mu=arange(0.01,20.,0.03)
info1=[]
info2=[]
for i in range(0,7):

    inf1=[]
    inf2=[]
    pathe = path +"%i/"%(i)

    nameplot= "time_traces_%d.pdf"%(i)

    pp=PdfPages(nameplot)

    for j in mu:

        filename="InfoDati_"+"%lf_"%(j)+"%d.txt"%(i)



        fil = pathe + filename

        data=loadtxt(fil)

        t=data[:,0]
        x=data[:,1]
        y=data[:,2]             # y, z should be the simulation, x the sin
        z=data[:,3]

        x=array(x)

        y=array(y)

        z=array(z)

        t=array(t)


        fig=filename

        fig=plt.figure()
        plt.plot(t,x,t,y,t,z)

        plt.xlim(0,500)
        pp.savefig(fig)

        pp.close()
        `

如果现在我输入plt.show()我有数百万的数字..我需要退出plt.plot的队列,或类似的东西!

最大的问题是,对于很多情节来说,队列变得如此之大以至于一切都崩溃了,终端说“杀死”并退出ipython .

谢谢您的帮助!

1 回答

  • 1

    我不确定我是否理解这个问题,但是这段代码表明你不确定你在做什么,所以这可能是错误所在:

    fig=filename # useless
    
        fig=plt.figure() # you're creating a figure that you never destroy
        plt.plot(t,x,t,y,t,z)
    
        plt.xlim(0,500)
        pp.savefig(fig)
    
        pp.close() # this shouldn't be inside this loop
    
    • 没有理由将 filename 分配给 fig ,因为在下一行中您正在将 plt.figure() 分配给同一个变量

    • 使用 pp.close() 时,即使 pp 在其外部创建,也会在循环内关闭pdf文件're currently saving your images to. But you' .

    • 这导致结论是您要将每个图像附加到PDF文件,然后关闭已打开的图像(在循环的下一步中创建新图像之前) . 所以你可能想要做 plt.close(fig) .

    • 由于您发布的代码从未关闭您创建的这些图像,这也可以解释为什么您有太多打开的图像 .

    尝试删除该额外的行,将 pp.close() 移动到循环后的某个位置,在循环中添加 plt.close(fig) 并查看是否可以解决您的问题 .

相关问题