For those coming across this question via a search engine of some sort: I've recently developed a comprehensive package to help with this: [matplotlib-map-utils][1].
It features a dedicated function (`scale_bar()`) and class (`ScaleBar`) for creating these objects with tons of customization features.
```py
# Importing
from matplotlib_map_utils.core.scale_bar import ScaleBar, scale_bar
# Setting up a plot
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
# Adding a scale bar to the upper-right corner of the axis,
# in the same projection as whatever geodata you plotted
# Here, this scale bar will have the "boxes" style
scale_bar(ax=ax, location="upper right", style="boxes", bar={"projection":3857})
```
[![rendered scale bar on a plot][2]][2]
This doesn't really scratch the surface on how much everything can be customized: check out `docs\howto_scale_bar` in the [GitHub repo][1] to see more.
[1]: https://github.com/moss-xyz/matplotlib-map-utils
[2]: https://i.sstatic.net/LgaWq6drm.png
For those coming across this question via a search engine of some sort: I've recently developed a comprehensive package to help with this: [matplotlib-map-utils][1].
It features a dedicated function (`scale_bar()`) and class (`ScaleBar`) for creating these objects with tons of customization features.
```py
# Importing
from matplotlib_map_utils.core.scale_bar import ScaleBar, scale_bar
# Setting up a plot
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
# Adding a scale bar to the upper-right corner of the axis,
# in the same projection as whatever geodata you plotted
# Here, this scale bar will have the "boxes" style
scale_bar(ax=ax, location="upper right", style="boxes", bar={"projection":3857})
```
[![rendered scale bar on a plot][2]][2]
This doesn't really scratch the surface on how much everything can be customized: check out `docs\howto_scale_bar` in the [GitHub repo][1] to see more.
[1]: https://github.com/moss-xyz/matplotlib-map-utils
[2]: https://i.sstatic.net/LgaWq6drm.png