Matplotlib 底图:移除海洋
Matplotlib Basemap: Removing Ocean
我有一个网格图,我想在其上叠加底图中的大陆。我正在使用此代码:
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,我的要求正好相反。我希望大陆位于网格图或我拥有的图像之上,因此只能看到海洋区域的网格。
您的要求:
- 网格图上方有大陆的图and/or 图像
- 只有mesh/image海域可见
要获取绘图,您必须在涉及的每个图层中使用 '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
一些有用的代码片段:
# 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)
我有一个网格图,我想在其上叠加底图中的大陆。我正在使用此代码:
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,我的要求正好相反。我希望大陆位于网格图或我拥有的图像之上,因此只能看到海洋区域的网格。
您的要求:
- 网格图上方有大陆的图and/or 图像
- 只有mesh/image海域可见
要获取绘图,您必须在涉及的每个图层中使用 '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
一些有用的代码片段:
# 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)