我正在迭代一大串字符串,并使用matplotlib的PdfPages后端为每个字符串创建一个PDF文件。当我一次只做一次(而不是遍历所有字符串)时,它工作得很好。然而,当我迭代的时候,我得到了很多不符合我传说中任何东西的额外行,这真的让我很困惑。我怀疑这是因为我需要的东西在附近徘徊的问题。下面是我正在写的伪代码:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def main():
stringList = import(stringList) #Not an actual function, this is just where I get my list
for s in stringList:
pp = PdfPages(s+'.pdf')
plotData(s, pp)
pp.close()
def plotData(s, pp):
dataList1, dataList2 = importData(s) #Again, not an actual function, just getting my data
#Make first figure
firstFig = plt.figure("first")
firstax = firstFig.add_subplot(111)
firstFig.suptitle("First")
xaxis = [x for x in range(len(dataList1))]
firstax.plot(xaxis, dataList2, label='local max')
tempax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=2, mode="expand", borderaxespad=0.)
pp.savefig()
#Make second figure
#Exactly the same thing....
#Make third figure
#Exactly the same thing...同样,我认为它与我如何使用这些对象有关,而且文档非常枯燥(我保证我已经看了一整天),但是如果有人知道这里发生了什么,我会很感激的!
发布于 2014-05-28 12:58:26
通过为plt.figure()指定一个字符串作为参数,您将在循环中每次重复使用相同的数字。处理这一问题的最简单方法就是不指定字符串并使用
fig = plt.figure()如果能把一个
plt.close('all')在每个循环的底部释放对图对象的所有引用。
https://stackoverflow.com/questions/23897901
复制相似问题