Matplotlib:在多个线程中同时绘图 [英] Matplotlib: simultaneous plotting in multiple threads

查看:649
本文介绍了Matplotlib:在多个线程中同时绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试并行进行一些绘图,以更快地完成大型批处理作业.为此,我为计划绘制的每个图启动了一个线程.

I am trying to do some plotting in parallel to finish large batch jobs quicker. To this end, I start a thread for each plot I plan on making.

我希望每个线程都能完成其绘制并关闭自身(据我所知,Python在通过run()中的所有语句时会关闭线程).下面是显示此行为的一些代码.

I had hoped that each thread would finish its plotting and close itself (as I understand it, Python closes threads when they get through all the statements in run()). Below is some code that shows this behavior.

如果将创建图形的行注释掉,它将按预期运行.另一个可能有用的提示是,当您仅生成一个线程时,它也可以按预期运行.

If the line that creates a figure is commented out, it runs as expected. Another plausibly helpful tidbit is that it also runs as expected when you only spawn one thread.

import matplotlib.pyplot as plt
import time
import Queue
import threading

def TapHistplots():
    ##  for item in ['str1']:
# # it behaves as expected if the line above is used instead of the one below
    for item in ['str1','str2']:
        otheritem = 1
        TapHistQueue.put((item, otheritem))
        makeTapHist().start()

class makeTapHist(threading.Thread):
    def run(self):
        item, otheritem = TapHistQueue.get()
        fig = FigureQueue.get()
        FigureQueue.put(fig+1)
        print item+':'+str(fig)+'\n',
        time.sleep(1.3)
        plt.figure(fig) # comment out this line and it behaves as expected
        plt.close(fig)

TapHistQueue = Queue.Queue(0)
FigureQueue = Queue.Queue(0)
def main():
    start = time.time()
    """Code in here runs only when this module is run directly"""
    FigureQueue.put(1)
    TapHistplots()
    while threading.activeCount()>1:
        time.sleep(1)
        print 'waiting on %d threads\n' % (threading.activeCount()-1),
    print '%ds elapsed' % (time.time()-start)

if __name__ == '__main__':
    main()

任何帮助都将受到感激.

Any help is duly appreciated.

推荐答案

为什么不只使用多处理?据我从您的描述可以看出,无论如何,线程化对您无济于事...

Why not just use multiprocessing? As far as I can tell from your description, threading won't help you much, anyway...

Matplotlib已经线程化,因此您可以一次显示多个图形并与之交互.如果要在多核计算机上加快批处理的速度,则无论如何都将需要多处理.

Matplotlib already threads so that you can display and interact with multiple figures at once. If you want to speed up batch processing on a multicore machine, you're going to need multiprocessing regardless.

作为一个基本示例(警告:这将在您运行它的任何目录中创建20个小的.png文件!)

import multiprocessing
import matplotlib.pyplot as plt
import numpy as np

def main():
    pool = multiprocessing.Pool()
    num_figs = 20
    input = zip(np.random.randint(10,1000,num_figs), 
                range(num_figs))
    pool.map(plot, input)

def plot(args):
    num, i = args
    fig = plt.figure()
    data = np.random.randn(num).cumsum()
    plt.plot(data)
    plt.title('Plot of a %i-element brownian noise sequence' % num)
    fig.savefig('temp_fig_%02i.png' % i)

main()

这篇关于Matplotlib:在多个线程中同时绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆