我正在尝试做一个以全局地图为背景的等高线图。考虑到我的数据具有LON和LAT值,我决定将Cartopy与MatplotLib结合使用。
问题是,当我将数据和地图分开时,我可以完美地绘制它们,但当我尝试将数据与地图集成时,Cartopy地图会覆盖我的数据绘制。
这是我的代码:
ax = plt.axes(projection=cartopy.crs.PlateCarree())
v = np.linspace(0, 80, 25, endpoint=True)
cp = plt.contourf(matrixLon, matrixLat, matrixTec, v, transform=cartopy.crs.PlateCarree())
plt.colorbar(cp)
ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.set_extent([-85, -30, -60, 15])
plt.title('TEC Map')
plt.show()图:
这很奇怪,因为我认为逻辑上是数据覆盖地图(也许我必须尝试透明色标),而不是相反。
有人能帮我解决这个问题吗?
发布于 2017-07-12 13:01:37
以下是您可以尝试和学习的工作代码。
import matplotlib.pyplot as plt
#import cartopy.crs as ccrs
import numpy as np
import cartopy
# prep some data for contourf plot
# extents: upper-right of the map
x = np.linspace(-65, -30, 30)
y = np.linspace(-30, 15, 30)
matrixLon, matrixLat = np.meshgrid(x, y)
matrixTec = 10*np.sin(matrixLon**2 + matrixLat**2)/(matrixLon**2 + matrixLat**2)
ax = plt.axes(projection=cartopy.crs.PlateCarree())
# prep increasing values of v covering values of Z (matrixTec)
v = np.arange(-0.15, 0.15, 0.025)
# plot with appropriate parameters
# zorder: put the filled-contour on top
# alpha: set transparency to allow some visibility of graphics below
cp = plt.contourf(matrixLon, matrixLat, matrixTec, v, \
transform=cartopy.crs.PlateCarree(), \
zorder=2, \
alpha=0.65, \
cmap=plt.cm.copper)
plt.colorbar(cp)
ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.set_extent([-85, -30, -60, 15])
plt.title('TEC Map')
plt.show()其实质是在plt.contourf()中使用zorder和alpha,可以设置它们来显示或隐藏地图上的某些要素。

https://stackoverflow.com/questions/45042722
复制相似问题