5

I am producing thousands of png files, using code similar to the following:

import matplotlib.pyplot as plt

...

for pd in plot_data:    
  fig = plt.figure(figsize=(5,5))
  ax = plt.axes(projection=crs.Mercator())
  ax.background_patch.set_facecolor((198/255, 236/255, 253/255))
  norm = colors.LogNorm(vmin=(min(data[pd])), vmax=(max(data[pd]))) 
  sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm)
  sm.set_array([])

  for i, p in enumerate(polygons): 
    ax.add_patch(PolygonPatch(p,facecolor=colormap(data[pd][i]), transform=ccrs.PlateCarree())

  ax.set_extent([west, east, south, north], crs=crs.PlateCarree())
  ax.set_facecolor((198/255, 236/255, 253/255))
  cb = plt.colorbar(sm) 

  plt.title(name)
  fig.savefig(pd+".png")
  plt.close()

There are 1000s of polygons in each map. Each iteration of the main loop takes approximately 35 seconds to execute. The for loop adding the polygons takes 5 seconds to execute while fig.savefig(pd+".png") takes 30 seconds. I was wondering if it would be possible for me to run fig.savefig(pd+".png") in its own thread so as to reduce this bottle neck. How can I investigate if this is possible?

Baz
  • 12,713
  • 38
  • 145
  • 268
  • 1
    I'm not sure if threading will help you here - it will at least not speed up the code. However, you might gain something using multiprocessing in case you have several cores at your disposal. Relevant here: [matplotlib savefig performance, saving multiple pngs within loop](https://stackoverflow.com/questions/41037840/matplotlib-savefig-performance-saving-multiple-pngs-within-loop). Also I would consider not adding 1000 individual polygons, but instead work with a `PathCollection` or `PolyCollection`. – ImportanceOfBeingErnest Jan 01 '19 at 18:12
  • Can you compromise with the quality of the figure? The `fig.savefig()` takes `dpi` as the default figure's `dpi`, we can change it to low quality by reducing the dpi. The common value is `100dpi`. – Mohammed Shammeer Jul 22 '22 at 21:44

0 Answers0