Skip to content

Instantly share code, notes, and snippets.

@timtroendle
Created April 26, 2018 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timtroendle/0029336764c0db2cb6f2832e08f51789 to your computer and use it in GitHub Desktop.
Save timtroendle/0029336764c0db2cb6f2832e08f51789 to your computer and use it in GitHub Desktop.
Prototype of plotting matplotlib plots in consecutive processes.
"""Prototype of plotting matplotlib plots in consecutive processes.."""
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from multiprocessing import Pool
# avoid to import pyplot as it'll import (on my machine) backends that fail with multiprocessing.
def do_plot(number):
fig = Figure(figsize=(13, 7))
FigureCanvas(fig)
ax = fig.add_subplot(1, 1, 1)
ax.plot([0, 1, 2, 3], [-1, 1, 0, 2])
fig.savefig("yo-plot{}.png".format(number))
if __name__ == '__main__':
with Pool(4) as pool:
for result in [pool.apply_async(do_plot, (id,)) for id in range(10)]:
result.wait()
print("All applied.")
pool.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment