Here's the Cartopy scale scale feature that I wrote for my own use, which uses a simpler version of pp-mo's answer: Edit: modified code to create a new projection centered so that the scale scale is parallel to the axes for many coordinate systems, including some spelling and large maps, and eliminating the need to specify a utm system. Also added a code to calculate the length of the scale, if it is not specified.
import cartopy.crs as ccrs import numpy as np def scale_bar(ax, length=None, location=(0.5, 0.05), linewidth=3): """ ax is the axes to draw the scalebar on. length is the length of the scalebar in km. location is center of the scalebar in axis coordinates. (ie. 0.5 is the middle of the plot) linewidth is the thickness of the scalebar. """
It has some limitations, but is relatively simple, so I hope you could see how to change it if you want something else.
Usage example:
import matplotlib.pyplot as plt ax = plt.axes(projection=ccrs.Mercator()) plt.title('Cyprus') ax.set_extent([31, 35.5, 34, 36], ccrs.Geodetic()) ax.coastlines(resolution='10m') scale_bar(ax, 100) plt.show()

Siyh
source share