I have a list of trajectories (various lengths) which I want to (overlay) plot on the same figure and then show/save it.
This can be done by running a simple for loop and calling plt.plot()
for each trajectory, and in the end running plt.show()
.
(using import matplotlib.pyplot as plt
)
This is very slow for a large number of trajectories (>10k) so I would like to parallelize the plots, but still have one single plot in the end.
I have found several posts that achieve this but making plots in each process. However, this is not what I need, as the trajectories all need to be in one figure.
I have also tried implementing a following approach:
import matplotlib.pyplot as plt
import multiprocessing as mpi
class WorkerClass():
def __init__(self, ax):
self.ax = ax
def worker(self, tli):
t,l,i = tli
self.ax.plot(t[:,0], t[:,1], alpha=0.5, color='C0' if l==-1 else 'C1', linestyle=np.array(['-','-.',':','--',':'])[i.astype(bool)][0])
def get_ax(self):
return self.ax
### main code
# trajectories, labels, info are lists passed from other functions
fig, ax = plt.subplots()
w = Worker(ax)
with mpi.Pool(processes=4) as p:
lines = p.map(w.worker, zip(trajectories, labels, info))
ax = w.get_ax()
ax.set_title('Trajectories')
plt.show()
However this doesn't work, I get a stack error:
Exception in thread Thread-3:
Traceback (most recent call last):
File "/home/robin/anaconda3/envs/py35lab/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/home/robin/anaconda3/envs/py35lab/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/home/robin/anaconda3/envs/py35lab/lib/python3.5/multiprocessing/pool.py", line 429, in _handle_results
task = get()
File "/home/robin/anaconda3/envs/py35lab/lib/python3.5/multiprocessing/connection.py", line 251, in recv
return ForkingPickler.loads(buf.getbuffer())
File "/home/robin/anaconda3/envs/py35lab/lib/python3.5/site-packages/matplotlib/figure.py", line 1904, in __setstate__
mgr = plt._backend_mod.new_figure_manager_given_figure(num, self)
File "/home/robin/anaconda3/envs/py35lab/lib/python3.5/site-packages/matplotlib/backends/_backend_tk.py", line 1044, in new_figure_manager_given_figure
window = Tk.Tk(className="matplotlib")
File "/home/robin/anaconda3/envs/py35lab/lib/python3.5/tkinter/__init__.py", line 1868, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: out of stack space (infinite loop?)