matplotlib中的多进程绘图

编程入门 行业动态 更新时间:2024-10-11 13:22:33
本文介绍了matplotlib中的多进程绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何通过并行函数使用matplotlib可视化数据? IE.我想在并行流程中创建图形,然后在主流程中显示它们.

How can one visualize data using matplotlib by a function in parallel? I.e. I want to create figures in parallel processes and then display them in the main process.

这里是一个例子:

# input data import pandas as pd, matplotlib.pyplot as plt df = pd.DataFrame(data={'i':['A','A','B','B'], 'x':[1.,2.,3.,4.], 'y':[1.,2.,3.,4.]}) df.set_index('i', inplace=True) df.sort_index(inplace=True) # function which creates a figure from the data def Draw(df, i): fig = plt.figure(i) ax = fig.gca() df = df.loc[i,:] ax.scatter(df['x'], df['y']) return fig def DrawWrapper(x): return Draw(*x) # creating figures in parallel from multiprocessing import Pool poolSize = 2 with Pool(poolSize) as p: args = [(df,'A'), (df,'B')] figs = p.map(DrawWrapper, args) # attempt to visualize the results fig = plt.figure('A') plt.show() # FIXME: get "RuntimeError: main thread is not in main loop"

如何从工作流程中转移图形对象,以便能够在主流程中显示图形?

How do I transfer the figure objects from the worker processes such as to be able to show the figures in the main process?

谢谢您的帮助!

建议该问题可以通过此线程

It was suggested that the problem might be solved by this thread

这是对应的代码:

# input data import pandas as pd, matplotlib.pyplot as plt df = pd.DataFrame(data={'i':['A','A','B','B'], 'x':[1.,2.,3.,4.], 'y':[1.,2.,3.,4.]}) df.set_index('i', inplace=True) df.sort_index(inplace=True) # function which creates a figure from the data def Draw(df, i): fig = plt.figure(i) ax = fig.gca() df = df.loc[i,:] ax.scatter(df['x'], df['y']) plt.show() # creating figures in parallel from multiprocessing import Process args = [(df,'A'), (df,'B')] for a in args: p = Process(target=Draw, args=a) p.start() # FIXME: result is the same (might be even worse since I do not # get any result which I could attempt to show): # ... # RuntimeError: main thread is not in main loop # RuntimeError: main thread is not in main loop

我想念什么吗?

推荐答案

链接的问题的答案在if __name__ == "__main__":子句中隐藏了代码的开头.因此,以下内容应在这里工作.

The linked question's answer hides the start of the code in a if __name__ == "__main__": clause. Hence the following should work here.

import pandas as pd import matplotlib.pyplot as plt import multiprocessing #multiprocessing.freeze_support() # <- may be required on windows df = pd.DataFrame(data={'i':['A','A','B','B'], 'x':[1.,2.,3.,4.], 'y':[1.,2.,3.,4.]}) df.set_index('i', inplace=True) df.sort_index(inplace=True) # function which creates a figure from the data def Draw(df, i): fig, ax = plt.subplots() df = df.loc[i,:] ax.scatter(df['x'], df['y']) plt.show() # creating figures in parallel args = [(df,'A'), (df,'B')] def multiP(): for a in args: p = multiprocessing.Process(target=Draw, args=a) p.start() if __name__ == "__main__": multiP()

更多推荐

matplotlib中的多进程绘图

本文发布于:2023-07-18 15:45:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1146357.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程   matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!