How to draw rectangles on a Basemap

前端 未结 3 1492
暖寄归人
暖寄归人 2020-12-09 10:34

I\'m looking for a way to plot filled rectangles on a Basemap. I could easily draw the rectangle\'s edges using the drawgreatcircle method, but I cannot find a

3条回答
  •  猫巷女王i
    2020-12-09 10:54

    Using Andrew's answer, I get the error

    TypeError: len() of unsized object.
    

    However, casting the zip to a list fixes this.

    Complete code:

    from mpl_toolkits.basemap import Basemap
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib.patches import Polygon
    
    def draw_screen_poly( lats, lons, m):
        x, y = m( lons, lats )
        xy = zip(x,y)
        poly = Polygon( list(xy), facecolor='red', alpha=0.4 )
        plt.gca().add_patch(poly)
    
    lats = [ -30, 30, 30, -30 ]
    lons = [ -50, -50, 50, 50 ]
    
    m = Basemap(projection='sinu',lon_0=0)
    m.drawcoastlines()
    m.drawmapboundary()
    draw_screen_poly( lats, lons, m )
    
    plt.show()
    

提交回复
热议问题