使用 savefig 的 Matplotlib 多处理字体损坏

Tom*_*mas 5 python matplotlib multiprocessing

自 1.5 版以来,我在 Matplotlib 中的多处理遇到了麻烦。字体在其原始位置周围随机跳跃。示例在这里: 在此处输入图片说明

重现此错误的简单示例如下:

import multiprocessing
import matplotlib.pyplot as plt

fig = plt.figure()

def plot(i):
    fig = plt.gcf()
    plt.plot([],[])
    fig.savefig('%d.png' % i)

plot(0)
pool = multiprocessing.Pool(4)
pool.map(plot, range(10))
Run Code Online (Sandbox Code Playgroud)

如果多处理和简单绘图的顺序颠倒

pool = multiprocessing.Pool(4)
plot(0)
pool.map(plot, range(10))
Run Code Online (Sandbox Code Playgroud)

然后它起作用了,但是这种解决方法对我的目的没有用。

谢谢你。

小智 2

我最近在测试并行绘制大量图的方法时遇到了同样的问题。虽然我还没有找到使用多处理模块的解决方案,但我发现使用并行Python包(http://www.parallelpython.com/)没有看到相同的错误。在我的早期测试中,它似乎比多处理模块慢约 50%,但仍然比串行绘图有显着的加速。关于模块导入也有点挑剔,所以我最终更愿意找到使用多处理的解决方案,但目前这是一个还可以的解决方法(至少对我来说)。也就是说,我对并行处理还很陌生,因此我在这里可能缺少这两种方法的一些细微差别。

###############################################################################
import os
import sys
import time
#import numpy as np
import numpy    # Importing with 'as' doesn't work with Parallel Python
#import matplotlib.pyplot as plt
import matplotlib.pyplot    # Importing with 'as' doesn't work with Parallel Python
import pp
import multiprocessing as mp
###############################################################################
path1='./Test_PP'
path2='./Test_MP'
nplots=100
###############################################################################
def plotrandom(plotid,N,path):
    numpy.random.seed() # Required for multiprocessing module but not Parallel Python...
    x=numpy.random.randn(N)
    y=x**2
    matplotlib.pyplot.scatter(x,y)
    matplotlib.pyplot.savefig(os.path.join(path,'test_%d.png'%(plotid)),dpi=150)
    matplotlib.pyplot.close('all')
##############################################################################    #
# Parallel Python implementation
tstart_1=time.time()
if not os.path.exists(path1):
    os.makedirs(path1)

ppservers = ()

if len(sys.argv) > 1:
    ncpus = int(sys.argv[1])
    job_server = pp.Server(ncpus, ppservers=ppservers)
else:
    job_server = pp.Server(ppservers=ppservers)

print "Starting Parallel Python v2 with", job_server.get_ncpus(), "workers"

jobs = [(input_i, job_server.submit(plotrandom,(input_i,10,path1),(),("numpy","matplotlib.pyplot"))) for input_i in range(nplots)]

for input_i, job in jobs:
    job()

tend_1=time.time()
t1=tend_1-tstart_1
print 'Parallel Python = %0.5f sec'%(t1)
job_server.print_stats()
##############################################################################    #
# Multiprocessing implementation
tstart_2=time.time()
if not os.path.exists(path2):
    os.makedirs(path2)

if len(sys.argv) > 1:
    ncpus = int(sys.argv[1])
else:
    ncpus = mp.cpu_count()

print "Starting multiprocessing v2 with %d workers"%(ncpus)

pool = mp.Pool(processes=ncpus)
jobs = [pool.apply_async(plotrandom, args=(i,10,path2)) for i in range(nplots)]
results = [r.get() for r in jobs]    # This line actually runs the jobs
pool.close()
pool.join()

tend_2=time.time()
t2=tend_2-tstart_2
print 'Multiprocessing = %0.5f sec'%(t2)
###############################################################################
Run Code Online (Sandbox Code Playgroud)