如何通过并行函数使用matplotlib可视化数据?也就是说,我想在并行过程中创建图形,然后在主过程中显示它们。
这里有一个例子:
# 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"
如何从辅助流程中转移图形对象,以便能够在主流程中显示图形?
谢谢你的帮助!
[EDIT:]有人建议这个线程可以解决这个问题
这是相应的代码:
# 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__":
子句中代码的开头。因此,以下内容在这里应该有效。
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()