Cartopy + Matplotlib(contourf) - 映射覆盖数据

时间:2017-07-11 19:08:53

标签: python matplotlib cartopy

我正在尝试在背景中创建具有全局地图的轮廓图。 考虑到我的数据具有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()

图:

Plotting only Data

Plotting only the map

这很奇怪,因为我认为逻辑是数据覆盖地图(也许我必须尝试透明色标)但不是相反。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

以下是您可以尝试学习的工作代码。

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()中使用zorderalpha,可以将其设置为在地图上显示或隐藏某些功能。

enter image description here