【问题标题】:Matplotlib Basemap: Removing OceanMatplotlib 底图:去除海洋
【发布时间】:2020-05-08 01:17:07
【问题描述】:

我有一个网格图,我想在其上覆盖底图中的大陆。我正在使用此代码:

       from mpl_toolkits.basemap import Basemap
       import matplotlib.pyplot as plt
       m = Basemap(width=12000000,height=9000000,projection='lcc',
            resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)

       m.drawlsmask(land_color='coral',ocean_color='aqua',lakes=True)
       plt.show()

参考this,我的要求正好相反。我想要在我拥有的网格图或图像之上的大陆,所以只有海洋区域的网格是可见的。

【问题讨论】:

    标签: python overlay matplotlib-basemap


    【解决方案1】:

    您的要求:

    1. 在网格图和/或图像之上包含大陆的图
    2. 只有海洋区域的网格/图像可见

    要获得绘图,您必须在所涉及的每个层中使用“zorder”。要绘制的数据必须进行适当的转换。这是您可以尝试的代码,以及它产生的输出图。

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    import numpy as np
    
    m = Basemap(projection='lcc', width=12000000, height=9000000, 
        resolution='c', lat_1=45., lat_2=55, lat_0=50, lon_0=-107.)
    
    # if resolution is None, coastlines wont draw
    
    # draw only land areas with zorder=20
    # any other layers with zorder below 20 will be hidden by land areas
    m.drawlsmask(land_color='coral', ocean_color='none', lakes=True, zorder=20)
    m.drawcoastlines(linewidth=0.3, color='gray', zorder=25)
    
    filename = "small_01.png"  #use your image here
    lonmin, lonmax, latmin, latmax = (-130, -40, 35, 45) # set limits of the image
    
    # compute the limits of the image in data coordinates
    left, bottom = m (lonmin, latmin)
    top, right = m(lonmax, latmax)
    image_extent = (left, right, bottom, top)
    
    ax = plt.gca()
    # set zorder < 20, to plot the image below land areas
    ax.imshow(plt.imread(filename), extent=image_extent, zorder=15)
    
    # plot some meshgrid data
    # set zorder above image, but below land
    xs = np.linspace(-130, -60, 20)
    ys = np.linspace(20, 60, 10)
    x2d, y2d = np.meshgrid(xs, ys)
    
    #ax.plot(*m(x2d, y2d), 'ro', zorder=16)  # faster
    ax.scatter(*m(x2d, y2d), s=2, zorder=16)
    
    plt.show()
    

    编辑 1

    一些有用的代码sn-p:

    # This plots shaded relief terrain covering land and sea.
    m.shadedrelief(zorder = 25)
    
    # This plots only ocean/sea parts on top.
    m.drawlsmask(land_color='none', ocean_color='aqua', zorder=26)
    

    【讨论】:

    • 这正是我所需要的。因此,当您将海洋颜色设置为“无”时,它会移除海洋,对吗?所以下面的任何东西都可以被 zorder 查看。
    • 还有一件事,可以将土地颜色改为阴影浮雕而不是素色吗?使用m.shadedrelief() 可以涵盖所有内容。
    • @GENIVI-LEARNER 是的。请参阅编辑后的答案和示例图。
    • 完美。这就是我所需要的!
    • 方便的话请check this
    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 2015-12-27
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多