I'm trying to create a subplot of several pcolormesh graphs that would be like this one:
I achieve to do this by computing on one process:
import matplotlib.pyplot as plt
import numpy as np
def create_spectrogram(data, x, y, ax):
ax.pcolormesh(x, y, data)
def do_it_simple():
size = 10
data = np.arange(size * size).reshape((size, size))
y = np.arange(0, 10)
x = np.arange(0, 10)
fig, axes = plt.subplots(nrows=2, ncols=5)
axes_list = [item for sublist in axes for item in sublist]
for ax in axes_list:
create_spectrogram(data, x, y, ax=ax)
plt.savefig("test_simple.png")
def main():
do_it_simple()
Assuming my data array is really big, there are two very slow steps:
- ax.pcolormesh(x, y, data)
- plt.savefig()
I was wondering if I could use multiprocessing to parallelize the pcolormesh process. Now I have:
import matplotlib.pyplot as plt
import numpy as np
from multiprocessing import Pool
from functools import partial
def multiprocesser_handler(data_set, ax):
(data, x, y) = data_set
ax.pcolormesh(x, y, data)
return ax
def do_it_multiprocessed():
size = 10
data = np.arange(size * size).reshape((size, size))
y = np.arange(0, 10)
x = np.arange(0, 10)
fig, axes = plt.subplots(nrows=2, ncols=5)
axes_list = [item for sublist in axes for item in sublist]
data_set = (data, x, y)
func = partial(multiprocesser_handler, data_set)
with Pool(3) as p:
axes_list = p.map(func, axes_list)
print("DEBUG1:", axes_list)
for ax in axes_list:
print("DEBUG2:",ax.get_children())
plt.savefig("test_multiprocess.png")
def main():
do_it_multiprocessed()
The results are:
DEBUG1 [<matplotlib.axes._subplots.AxesSubplot object at 0x7fa6335fc2e8>, <matplotlib.axes._subplots.AxesSubplot object at 0x7fa633524080>, ....
DEBUG2 [<matplotlib.collections.QuadMesh object at 0x7fa63129c278>, <matplotlib.spines.Spine object at 0x7fa6312f8dd8>, <matplotlib.spines.Spine object at 0x7fa63130d4e0>, <matplotlib.spines.Spine object at 0x7fa63130d5f8>, <matplotlib.spines.Spine object at 0x7fa631295fd0>, <matplotlib.axis.XAxis object at 0x7fa63130d668>, <matplotlib.axis.YAxis object at 0x7fa6312f8e48>, Text(0.5, 1.0, '2005'), Text(0.0, 1.0, ''), Text(1.0, 1.0, ''), <matplotlib.patches.Rectangle object at 0x7fa63129cb38>]
DEBUG2 [<matplotlib.collections.QuadMesh object at 0x7fa6334fdb70>, <matplotlib.spines.Spine object at 0x7fa633260438>, <matplotlib.spines.Spine object at 0x7fa63336f128>, <matplotlib.spines.Spine object at 0x7fa63336f1d0>, <matplotlib.spines.Spine object at 0x7fa6334fd908>, <matplotlib.axis.XAxis object at 0x7fa63336f550>, <matplotlib.axis.YAxis object at 0x7fa633260b00>, Text(0.5, 1.0, '2006'), Text(0.0, 1.0, ''), Text(1.0, 1.0, ''), <matplotlib.patches.Rectangle object at 0x7fa63352f470>]
...
It seems like I achieved to parallelize correctly the pcolormesh computation process (the result contains matplotlib.collections.QuadMesh
objects). But when I open the result figure I get this:
I guess that I do not merge all the AxesSubplot
correctly, any idea to do this ?
I also tried to use imshow() that is known to be more efficient than pcolormesh, but I had some issues that are described here: Change y log scale imshow()
Thanks in advance !