How to insert a scale on a map in matplotlib

Any ideas on how I can insert a scale scale on a map in matplotlib that shows a length scale? something like what i attached.

Or maybe any ideas on measuring and displaying distances automatically (without drawing an arrow and recording the distance manually!)?

Thanks:) enter image description here

+8
python matplotlib scale matplotlib-basemap
source share
2 answers

I would try the matplotlib-scalebar . (Something like your example c.)

Assuming you draw a map image using imshow or similar, and know the pixel width / cell size (the real equivalent size of one pixel on the map image), you can automatically create a scale scale

This example is directly on the PyPi matplotlib-scalebar page , but here for completeness:

 import matplotlib.pyplot as plt import matplotlib.cbook as cbook from matplotlib_scalebar.scalebar import ScaleBar plt.figure() image = plt.imread(cbook.get_sample_data('grace_hopper.png')) plt.imshow(image) scalebar = ScaleBar(0.2) # 1 pixel = 0.2 meter plt.gca().add_artist(scalebar) plt.show() 

enter image description here

+4
source share

There is already an existing class for scales in matplotlib called AnchoredSizeBar . In the example below, AnchoredSizeBar is used to add a scale to the image (or display a random area of โ€‹โ€‹100x100 meters).

 import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar import matplotlib.font_manager as fm fontprops = fm.FontProperties(size=18) fig, ax = plt.subplots() ax.imshow(np.random.random((10,10)),extent=[0,100,0,100]) 

The extent defines the max and min images of horizontal and vertical values.

 scalebar = AnchoredSizeBar(ax.transData, 20, '20 m', 'lower center', pad=0.1, color='white', frameon=False, size_vertical=1, fontproperties=fontprops) ax.add_artist(scalebar) 

The four first arguments to AnchoredSizeBar are the coordinate system transformation object, scale length, label, and location. Additional optional arguments change the layout. They are well explained in docstring.

 ax.set_yticks([]) ax.set_xticks([]) 

This gives a Scalebar on an image / map at a random distance of 100x100 meters

+4
source share

All Articles