Matplotlib底图:消除海洋 [英] Matplotlib Basemap: Removing Ocean

查看:124
本文介绍了Matplotlib底图:消除海洋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格图,要在底图上覆盖大洲.我正在使用此代码:

I have an meshplot onto which I want to overlay continents in basemap. I am using this code:

       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()

引用,我的要求是对面的.我希望在网格图或图像的顶部都具有大洲,因此只能看到海洋区域的网格.

referring to this, my requirements is opposite. I want continents ontop of the meshplot or image I have so only mesh at the ocean area is visible.

推荐答案

您的要求:

  1. 在网格图和/或图像上方具有大洲的图
  2. 仅可见海洋区域的网格/图像

要获取图表,必须在涉及的每个图层中使用"zorder".要绘制的数据必须适当地转换.这是您可以尝试使用的代码,以及它产生的输出图.

To get the plot, you must use 'zorder' in each of the layers involved. The data to plot must be transformed appropriately. Here is a code that you can try, and the output plot it produces.

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

一些有用的代码段:

# 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)

这篇关于Matplotlib底图:消除海洋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆