简体   繁体   English

散点图未使用Managers进行多重处理

[英]Scatter Plot not plotting using Managers for multiprocessing

This program shows no error yet it produces a blank plot without any points. 该程序没有显示错误,但是会生成没有任何点的空白图。 I later on wish to modify this code so that many processes can work in parallel to change the shared matplotlib scatter plot. 后来我希望修改此代码,以便许多进程可以并行工作以更改共享的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')

Maybe you should re draw the figure after you add the scatter 添加散点图后,也许您应该重新绘制图形

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM