world map without rivers with matplotlib / Basemap?

后端 未结 7 685
生来不讨喜
生来不讨喜 2021-01-31 10:37

Would there be a way to plot the borders of the continents with Basemap (or without Basemap, if there is some other way), without those annoying rivers coming along? Especially

7条回答
  •  遇见更好的自我
    2021-01-31 10:59

    Okay I think I have a partial solution.

    The basic idea is that the paths used by drawcoastlines() are ordered by the size/area. Which means the first N paths are (for most applications) the main land masses and lakes and the later paths the smaller islands and rivers.

    The issue is that the first N paths that you want will depend on the projection (e.g., global, polar, regional), if area_thresh has been applied and whether you want lakes or small islands etc. In other words, you will have to tweak this per application.

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    
    mp = 'cyl'
    m = Basemap(resolution='c',projection=mp,lon_0=0,area_thresh=200000)
    
    fill_color = '0.9'
    
    # If you don't want lakes set lake_color to fill_color
    m.fillcontinents(color=fill_color,lake_color='white')
    
    # Draw the coastlines, with a thin line and same color as the continent fill.
    coasts = m.drawcoastlines(zorder=100,color=fill_color,linewidth=0.5)
    
    # Exact the paths from coasts
    coasts_paths = coasts.get_paths()
    
    # In order to see which paths you want to retain or discard you'll need to plot them one
    # at a time noting those that you want etc. 
    for ipoly in xrange(len(coasts_paths)):
        print ipoly
        r = coasts_paths[ipoly]
        # Convert into lon/lat vertices
        polygon_vertices = [(vertex[0],vertex[1]) for (vertex,code) in
                            r.iter_segments(simplify=False)]
        px = [polygon_vertices[i][0] for i in xrange(len(polygon_vertices))]
        py = [polygon_vertices[i][1] for i in xrange(len(polygon_vertices))]
        m.plot(px,py,'k-',linewidth=1)
        plt.show()
    

    Once you know the relevant ipoly to stop drawing (poly_stop) then you can do something like this...

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    
    mproj = ['nplaea','cyl']
    mp = mproj[0]
    
    if mp == 'nplaea':
        m = Basemap(resolution='c',projection=mp,lon_0=0,boundinglat=30,area_thresh=200000,round=1)
        poly_stop = 10
    else:
        m = Basemap(resolution='c',projection=mp,lon_0=0,area_thresh=200000)
        poly_stop = 18
    fill_color = '0.9'
    
    # If you don't want lakes set lake_color to fill_color
    m.fillcontinents(color=fill_color,lake_color='white')
    
    # Draw the coastlines, with a thin line and same color as the continent fill.
    coasts = m.drawcoastlines(zorder=100,color=fill_color,linewidth=0.5)
    
    # Exact the paths from coasts
    coasts_paths = coasts.get_paths()
    
    # In order to see which paths you want to retain or discard you'll need to plot them one
    # at a time noting those that you want etc. 
    for ipoly in xrange(len(coasts_paths)):
        if ipoly > poly_stop: continue
        r = coasts_paths[ipoly]
        # Convert into lon/lat vertices
        polygon_vertices = [(vertex[0],vertex[1]) for (vertex,code) in
                            r.iter_segments(simplify=False)]
        px = [polygon_vertices[i][0] for i in xrange(len(polygon_vertices))]
        py = [polygon_vertices[i][1] for i in xrange(len(polygon_vertices))]
        m.plot(px,py,'k-',linewidth=1)
    plt.show()
    

    enter image description here

提交回复
热议问题