This page was generated from gallery/matplotlib_scalebar.ipynb.
Interactive online version: Binder badge

Adding a scale bar to a matplotlib plot

When making a geospatial plot in matplotlib, you can use maplotlib-scalebar library to add a scale bar.

import geopandas as gpd
from matplotlib_scalebar.scalebar import ScaleBar

Creating a ScaleBar object

The only required parameter for creating a ScaleBar object is dx. This is equal to a size of one pixel in real world. Value of this parameter depends on units of your CRS.

Projected coordinate system (meters)

The easiest way to add a scale bar is using a projected coordinate system with meters as units. Just set dx = 1:

nybb = gpd.read_file(gpd.datasets.get_path('nybb'))
nybb = nybb.to_crs(32619)  # Convert the dataset to a coordinate
# system which uses meters

ax = nybb.plot()
<matplotlib_scalebar.scalebar.ScaleBar at 0x7f89843505b0>

Geographic coordinate system (degrees)

With a geographic coordinate system with degrees as units, dx should be equal to a distance in meters of two points with the same latitude (Y coordinate) which are one full degree of longitude (X) apart. You can calculate this distance by online calculator (e.g. the Great Circle calculator) or in geopandas.
Firstly, we will create a GeoSeries with two points that have roughly the coordinates of NYC. They are located on the same latitude but one degree of longitude from each other. Their initial coordinates are specified in a geographic coordinate system (geographic WGS 84). They are then converted to a projected system for the calculation:
from shapely.geometry.point import Point

points = gpd.GeoSeries([Point(-73.5, 40.5), Point(-74.5, 40.5)], crs=4326)  # Geographic WGS 84 - degrees
points = points.to_crs(32619) # Projected WGS 84 - meters

After the conversion, we can calculate the distance between the points. The result slightly differs from the Great Circle Calculator but the difference is insignificant (84,921 and 84,767 meters):

distance_meters = points[0].distance(points[1])

Finally, we are able to use geographic coordinate system in our plot. We set value of dx parameter to a distance we just calculated:

nybb = gpd.read_file(gpd.datasets.get_path('nybb'))
nybb = nybb.to_crs(4326)  # Using geographic WGS 84

ax = nybb.plot()
<matplotlib_scalebar.scalebar.ScaleBar at 0x7f894bdace50>

Using other units

The default unit for dx is m (meter). You can change this unit by the units and dimension parameters. There is a list of some possible units for various values of dimension below:




km, m, cm, um


in, ft, yd, mi


1/m, 1/cm



In the following example, we will leave the dataset in its initial CRS which uses feet as units. The plot shows scale of 2 leagues (approximately 11 kilometers):

nybb = gpd.read_file(gpd.datasets.get_path('nybb'))

ax = nybb.plot()
ax.add_artist(ScaleBar(1, dimension="imperial-length", units="ft"))
<matplotlib_scalebar.scalebar.ScaleBar at 0x7f894bd84f70>

Customization of the scale bar

nybb = gpd.read_file(gpd.datasets.get_path('nybb')).to_crs(32619)
ax = nybb.plot()

# Position and layout
scale1 = ScaleBar(
dx=1, label='Scale 1',
    location='upper left',  # in relation to the whole plot
    label_loc='left', scale_loc='bottom'  # in relation to the line

# Color
scale2 = ScaleBar(
    dx=1, label='Scale 2', location='center',
    color='#b32400', box_color='yellow',
    box_alpha=0.8  # Slightly transparent box

# Font and text formatting
scale3 = ScaleBar(
    dx=1, label='Scale 3',
    font_properties={'family':'serif', 'size': 'large'},  # For more information, see the cell below
    scale_formatter=lambda value, unit: f'> {value} {unit} <'

<matplotlib_scalebar.scalebar.ScaleBar at 0x7f894bd2b160>
Note: Font is specified by six properties: family, style, variant, stretch, weight, size (and math_fontfamily). See more.
For more information about matplotlib-scalebar library, see the PyPI or GitHub page.