Skip to content

Instantly share code, notes, and snippets.

@gmaze
Created March 20, 2020 12:44
Show Gist options
  • Save gmaze/d605c291e8e8858b9b4ab03be10fbadc to your computer and use it in GitHub Desktop.
Save gmaze/d605c291e8e8858b9b4ab03be10fbadc to your computer and use it in GitHub Desktop.
Parallel figure generation in python
#!/usr/bin/env python
# coding: utf-8
#
# $ time ./Parallel_images.py
# Use 8 processes
# 107.249u 2.444s 0:17.10 641.4% 0+0k 0+0io 1056pf+0w
#
import os
import numpy as np
import multiprocessing
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.use('Agg')
N = 1000
def generate_one_figure(it=1):
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(10,5), dpi=80)
x = np.linspace(-np.pi, np.pi, N)
ax.grid(1)
ax.plot(x, np.sin(x))
ax.plot(x[it], np.sin(x[it]),'r+', markersize=12)
ax.set_title("%3d" % it)
fig.savefig(os.path.abspath(os.path.sep.join([".","dummy_images","image_%0.4d.png" % it])))
plt.close(fig)
return None
print('Use %i processes' % multiprocessing.cpu_count() )
with multiprocessing.Pool() as pool:
pool.map(generate_one_figure, np.arange(0,N))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment