matplotlib图形不会在多处理中保存

2024-10-01 02:31:09 发布

您现在位置:Python中文网/ 问答频道 /正文

在多处理生成的进程中保存matplotlib图形时遇到问题。根据大量的例子,我发现下面的测试代码应该生成一个名为:测试图png但是,当这段代码在ipython中运行时,png文件实际上从未创建过。在

    from multiprocessing import Process

    def makeplot():
        import matplotlib.pyplot as plt
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot([1,2,3],[3,4,5],'o')
        fig.savefig('testfig.png')
        plt.close()
        return None

    p = Process(target = makeplot)
    p.daemon = True
    p.start()
    p.join()

Tags: 文件代码import图形png进程matplotlibipython