How to draw rectangles on a Basemap

前端 未结 3 1479
暖寄归人
暖寄归人 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条回答
  • 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()
    
    0 讨论(0)
  • 2020-12-09 10:59

    Similar answer to above, but more basic code:

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    from matplotlib.patches import Polygon
    
    map = Basemap(projection='cyl')
    
    map.drawmapboundary(fill_color='aqua')
    map.fillcontinents(color='coral',lake_color='aqua')
    map.drawcoastlines()
    
    x1,y1 = map(-25,-25)
    x2,y2 = map(-25,25)
    x3,y3 = map(25,25)
    x4,y4 = map(25,-25)
    poly = Polygon([(x1,y1),(x2,y2),(x3,y3),(x4,y4)],facecolor='red',edgecolor='green',linewidth=3)
    plt.gca().add_patch(poly)
    
    plt.show()
    
    0 讨论(0)
  • 2020-12-09 11:15

    You can add a matplotlib.patches.Polygon() directly to your axes. The question is whether you want your rectangles defined the plot coordinates (straight lines on the plot) or in map coordinates (great circles on the plot). Either way, you specify vertices in map coordinates and then transform them to plot coordinates by calling the Basemap instance (m() in the below example), build a Polygon yourself, and add it manually to the axes to be rendered.

    For rectangles defined in plot coordinates, here's an example:

    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( 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()
    

    For rectangles defined in map coordinates, use the same approach, but interpolate your line in map space before transforming to plot coordinates. For each line segment, you'll have to do:

    lats = np.linspace( lat0, lat1, resolution )
    lons = np.linspace( lon0, lon1, resolution )
    

    Then transform these map coordinates to plot coordinates (as above, with m()) and again create a Polygon with the plot coordinates.

    0 讨论(0)
提交回复
热议问题