Plot only on continent in matplotlib

前端 未结 5 780
清歌不尽
清歌不尽 2020-11-27 07:13

I am drawing a map using basemap from matplotlib. The data are spreaded all over the world, but I just want to retain all the data on the continent and drop those on the oce

5条回答
  •  忘掉有多难
    2020-11-27 07:51

    The HYRY's answer won't work on new versions of matplotlib (nxutils is deprecated). I've made a new version that works:

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    from matplotlib.path import Path
    import numpy as np
    
    map = Basemap(projection='cyl', resolution='c')
    
    lons = [0., 0., 16., 76.]
    lats = [0., 41., 19., 51.]
    
    x, y = map(lons, lats)
    
    locations = np.c_[x, y]
    
    polygons = [Path(p.boundary) for p in map.landpolygons]
    
    result = np.zeros(len(locations), dtype=bool) 
    
    for polygon in polygons:
    
        result += np.array(polygon.contains_points(locations))
    
    print result
    

提交回复
热议问题