[英]Clean the queue of matplotlib, and save big pdf figures
I need to save different plots in the same pdf file, using data from different files. 我需要使用来自不同文件的数据将不同的图保存在同一pdf文件中。 I use this code that for each step open certain file analyze it using some function and save on the pdf file. 我使用此代码来打开每个步骤的某些文件,并使用某些功能对其进行分析并将其保存在pdf文件中。 My problem is that the queue of matplotlib increases and at the end if I say plt.show() I get back a huge number of different figures. 我的问题是matplotlib的队列增加了,如果我说plt.show()最后,我得到了大量不同的数字。 I do not want this, I would like to clean the queue of maplotlib without seeing the plots. 我不希望这样,我想在不查看图的情况下清理maplotlib队列。
The code that I use is: 我使用的代码是:
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()
`
if now I enter plt.show() I have back million of figures.. I need to quit the queue of plt.plot, or something like this! 如果现在输入plt.show(),我有上百万个数字。.我需要退出plt.plot队列,或类似的东西!
The big problem is that for a lot of plots, the queue becomes so big that everything crash and the terminal says 'Killed' and quit ipython. 最大的问题是,对于很多图而言,队列变得如此之大,以至于一切都崩溃了,并且终端显示“ Killed”并退出了ipython。
thanks for the help! 谢谢您的帮助!
I'm not sure if I understand the problem, but this part of your code suggests you're not sure about what you're doing, so this is probably where the error lies: 我不确定我是否理解问题,但是代码的这一部分表明您不确定自己在做什么,所以这可能是错误所在:
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
to fig
, since right in the next line you're assigning plt.figure()
to the same variable 没有理由将filename
分配给fig
,因为在下一行中,您正在将plt.figure()
分配给同一变量 pp.close()
, that closes the pdf file you're currently saving your images to. 当您使用pp.close()
,将关闭您当前将图像保存到的pdf文件。 But you're doing that inside the loop, even though pp
is created outside of it. 但是,即使在循环之外创建pp
,也要在循环内进行操作。 plt.close(fig)
instead. 因此,您可能想改做plt.close(fig)
。 Try removing that extra line, moving pp.close()
to somewhere after the loop, adding plt.close(fig)
inside the loop and see if that fixes your problem. 尝试删除多余的线,移动pp.close()
循环后的地方,加入plt.close(fig)
内环路,看看是否能解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.