world map without rivers with matplotlib / Basemap?

后端 未结 7 678
生来不讨喜
生来不讨喜 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条回答
  •  -上瘾入骨i
    2021-01-31 11:12

    How to remove "annoying" rivers:

    If you want to post-process the image (instead of working with Basemap directly) you can remove bodies of water that don't connect to the ocean:

    import pylab as plt
    A = plt.imread("world.png")
    
    import numpy as np
    import scipy.ndimage as nd
    import collections
    
    # Get a counter of the greyscale colors
    a      = A[:,:,0]
    colors = collections.Counter(a.ravel())
    outside_and_water_color, land_color = colors.most_common(2)
    
    # Find the contigous landmass
    land_idx = a == land_color[0]
    
    # Index these land masses
    L = np.zeros(a.shape,dtype=int) 
    L[land_idx] = 1
    L,mass_count = nd.measurements.label(L)
    
    # Loop over the land masses and fill the "holes"
    # (rivers without outlays)
    L2 = np.zeros(a.shape,dtype=int) 
    L2[land_idx] = 1
    L2 = nd.morphology.binary_fill_holes(L2)
    
    # Remap onto original image
    new_land = L2==1
    A2 = A.copy()
    c = [land_color[0],]*3 + [1,]
    A2[new_land] = land_color[0]
    
    # Plot results
    plt.subplot(221)
    plt.imshow(A)
    plt.axis('off')
    
    plt.subplot(222)
    plt.axis('off')
    B = A.copy()
    B[land_idx] = [1,0,0,1]
    plt.imshow(B)
    
    plt.subplot(223)
    L = L.astype(float)
    L[L==0] = None
    plt.axis('off')
    plt.imshow(L)
    
    plt.subplot(224)
    plt.axis('off')
    plt.imshow(A2)
    
    plt.tight_layout()  # Only with newer matplotlib
    plt.show()
    

    enter image description here

    The first image is the original, the second identifies the land mass. The third is not needed but fun as it ID's each separate contiguous landmass. The fourth picture is what you want, the image with the "rivers" removed.

提交回复
热议问题