Scatter Plot不使用Managers进行多处理绘图

时间:2017-01-01 18:52:18

标签: python matplotlib plot python-multiprocessing

此程序显示没有错误但它产生没有任何点的空白图。我后来希望修改此代码,以便许多进程可以并行工作以更改共享的matplotlib散点图。

from multiprocessing.managers import BaseManager, NamespaceProxy
from multiprocessing import Process
import matplotlib.pyplot as plt
import numpy as np

def plotter(res):
    print(res.ax,res.fig)
    res.ax.scatter(np.random.normal(0,20,(100,)),np.random.uniform(0,20,(100,)),s=1)
    res.fig.savefig('man2.png')


class PlotClass():
    fig = plt.figure()
    ax = fig.add_subplot(111)

    def __init__(self):
        print('Test') 


class MyManager(BaseManager):
    pass

class MyAttProxy(NamespaceProxy):
    # We need to expose the same __dunder__ methods as NamespaceProxy,
    _exposed_ = ('__getattribute__', '__setattr__', '__delattr__')

MyManager.register('Plot', PlotClass,MyAttProxy)

if __name__ == '__main__':
    manager = MyManager()
    manager.start()
    plotscat = manager.Plot()
    pr = Process(target=plotter,args=(plotscat,))
    pr.start()
    pr.join()
    plotscat.fig.savefig('fig.png')

1 个答案:

答案 0 :(得分:0)

也许你应该在添加散点图后重新绘制图形

def plotter(res):
    print(res.ax,res.fig)
    res.ax.scatter(np.random.normal(0,20,(100,)),np.random.uniform(0,20,(100,)),s=1)
    res.fig.canvas.draw_idle()
    res.fig.savefig('man2.png')