Cartopy/Matplotlib savefig slow for lcc projections

Viewed 81

We're in the process of migrating from Basemap to Cartoy and finding the savefig functionality to be a far too slow for our app, specifically when using the LambertConformal CRS. I ran a quick check using the the code snippet below and found that it takes about 10x as long to save using the LambertConformal projection vs PlateCarree. The plots within our app include numerous data layers including colormaps and contours but I found that savefig is consistently much slower when using the LCC CRS. I saw that others suggested users set PYPROJ_GLOBAL_CONTEXT=ON but since our app is multithreaded that isn't an option for us. We can adopt the PlateCarree CRS as our default if needed but I thought I would check to see if other users in the community had similar issues.

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import time

extent = [-10, 10, 50, 60]

lcc_projection = ccrs.LambertConformal(
    central_latitude=55,
    central_longitude=0,
)
pc_projection = ccrs.PlateCarree()

pc_plot = plt.axes(projection=pc_projection, facecolor="dimgrey")
pc_plot.set_extent(extent, crs=pc_projection)
pc_plot.add_feature(cfeature.LAND)

t = time.time()
plt.savefig('map_pc.png')
print(f"pc savefig time : {time.time() - t}") # Takes ~3s    

plt.close()

lcc_plot = plt.axes(projection=lcc_projection, facecolor="dimgrey")
lcc_plot.set_extent(extent, crs=pc_projection)
lcc_plot.add_feature(cfeature.LAND)

t = time.time()
plt.savefig('map_lcc.png')
print(f"lcc savefig time : {time.time() - t}")  # Takes ~30s     

plt.close()

Resulting images:

PC plot LCC Plot

0 Answers
Related