How to insert scale bar in a map in matplotlib

前端 未结 2 1724
生来不讨喜
生来不讨喜 2020-12-16 13:02

Any ideas on how can I insert a scale bar in a map in matplotlib that shows the length scale? something like the one I have attached.

Or maybe any ideas on measuring

2条回答
  •  一个人的身影
    2020-12-16 13:42

    I would try the matplotlib-scalebar package. (For something like your example c.)

    Assuming you are plotting a map image with imshow or similar, and you know the pixel width/cell-size (the real-world equivalent size of one pixel on the map image), you can automatically create the scale bar:

    This example is straight off the PyPi matplotlib-scalebar package page but here it is 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()
    

提交回复
热议问题