title: multiprocess plotting in matplotlib
body: <p>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.</p>
  
  <p>Here is an example:</p>
  
  
  
  <pre class="lang-py prettyprint-override"><code># 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"
  </code></pre>
  
  <p>How do I transfer the figure objects from the worker processes such as to be able to show the figures in the main process?</p>
  
  <p>Thank you for your help!</p>
  
  <p><strong>[EDIT:]</strong>
  It was suggested that the problem might be solved by <a href="https://stackoverflow.com/questions/43571924/multiprocessing-show-matplotlib-plot">this thread</a></p>
  
  <p>Here is the corresponding code:</p>
  
  
  
  <pre class="lang-py prettyprint-override"><code># 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
  </code></pre>
  
  <p>Am I missing something?</p>