How to increase Geodetic resolution / accuracy / smoothness in Cartopy
In our previous post we have detailed how to draw a geodetic in Cartopy. However, as you can see in the resulting map, the geodetic line is broken up into several clearly visible segments:
In order to fix that, we need to subclass the original projection of the plot. In this example, we’re subclassing ccrs.Mollweide()
:
import cartopy.crs as ccrs
class HighResMollweide(ccrs.Mollweide):
@property
def threshold(self): return 100.0
Note that the default threshold for the Mollweide projection is 100000.0
- you can check for yourself using print(ccrs.Mollweide().threshold)
Now we can use that projection in plt.axes()
:
ax = plt.axes(projection=HighResMollweide())
Complete example code
This code reproduced the high-resolution geodetic image shown above:
import cartopy.crs as ccrs
import cartopy.feature as cf
from matplotlib import pyplot as plt
class HighResMollweide(ccrs.Mollweide):
@property
def threshold(self): return 100.0
ax = plt.axes(projection=HighResMollweide())
ax.stock_img()
ax.add_feature(cf.BORDERS)
# Add geodetic between two points
# Format: plot([lon1, lon2], [lat1, lat2])
plt.plot([-75, 77.23], [43, 28.61],
color='blue', linewidth=2,
transform=ccrs.Geodetic()
)
# Make figure larger
plt.gcf().set_size_inches(20, 10)
# Save figure as SVG
plt.savefig("Cartopy-Geodetic-HiRes.svg")
Thanks to @ajdawson on StackOverflow for the original hint on how to solve this!