world map without rivers with matplotlib / Basemap?

后端 未结 7 753
生来不讨喜
生来不讨喜 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:58

    For reasons like this i often avoid Basemap alltogether and read the shapefile in with OGR and convert them to a Matplotlib artist myself. Which is alot more work but also gives alot more flexibility.

    Basemap has some very neat features like converting the coordinates of input data to your 'working projection'.

    If you want to stick with Basemap, get a shapefile which doesnt contain the rivers. Natural Earth for example has a nice 'Land' shapefile in the physical section (download 'scale rank' data and uncompress). See http://www.naturalearthdata.com/downloads/10m-physical-vectors/

    You can read the shapefile in with the m.readshapefile() method from Basemap. This allows you to get the Matplotlib Path vertices and codes in the projection coordinates which you can then convert into a new Path. Its a bit of a detour but it gives you all styling options from Matplotlib, most of which are not directly available via Basemap. Its a bit hackish, but i dont now another way while sticking to Basemap.

    So:

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    from matplotlib.collections import PathCollection
    from matplotlib.path import Path
    
    fig = plt.figure(figsize=(8, 4.5))
    plt.subplots_adjust(left=0.02, right=0.98, top=0.98, bottom=0.00)
    
    # MPL searches for ne_10m_land.shp in the directory 'D:\\ne_10m_land'
    m = Basemap(projection='robin',lon_0=0,resolution='c')
    shp_info = m.readshapefile('D:\\ne_10m_land', 'scalerank', drawbounds=True)
    ax = plt.gca()
    ax.cla()
    
    paths = []
    for line in shp_info[4]._paths:
        paths.append(Path(line.vertices, codes=line.codes))
    
    coll = PathCollection(paths, linewidths=0, facecolors='grey', zorder=2)
    
    m = Basemap(projection='robin',lon_0=0,resolution='c')
    # drawing something seems necessary to 'initiate' the map properly
    m.drawcoastlines(color='white', zorder=0)
    
    ax = plt.gca()
    ax.add_collection(coll)
    
    plt.savefig('world.png',dpi=75)
    

    Gives:

    enter image description here

提交回复
热议问题