我有一个轨迹列表(各种长度),我想要(重叠)在同一张图中绘制,然后显示/保存它。
这可以通过运行一个简单的for循环并为每个轨迹调用plt.plot()
,最后运行plt.show()
来完成。
(使用import matplotlib.pyplot as plt
)
这对于许多轨迹(> 10k)来说非常慢,所以我想并行化这些图,但最后仍然只有一个图。
我发现有几个posts实现了这一目标,但是在每个过程中都进行了绘制。但是,这不是我所需要的,因为所有轨迹都必须在一个图中。
我还尝试过以下方法:
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()
但是这不起作用,我得到一个堆栈错误:
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?)