首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >matplotlib PdfPages复合图像

matplotlib PdfPages复合图像
EN

Stack Overflow用户
提问于 2014-05-27 20:00:17
回答 1查看 1.7K关注 0票数 2

我正在迭代一大串字符串,并使用matplotlib的PdfPages后端为每个字符串创建一个PDF文件。当我一次只做一次(而不是遍历所有字符串)时,它工作得很好。然而,当我迭代的时候,我得到了很多不符合我传说中任何东西的额外行,这真的让我很困惑。我怀疑这是因为我需要的东西在附近徘徊的问题。下面是我正在写的伪代码:

代码语言:javascript
复制
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...

同样,我认为它与我如何使用这些对象有关,而且文档非常枯燥(我保证我已经看了一整天),但是如果有人知道这里发生了什么,我会很感激的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-28 12:58:26

通过为plt.figure()指定一个字符串作为参数,您将在循环中每次重复使用相同的数字。处理这一问题的最简单方法就是不指定字符串并使用

代码语言:javascript
复制
fig = plt.figure()

如果能把一个

代码语言:javascript
复制
plt.close('all')

在每个循环的底部释放对图对象的所有引用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23897901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档