我正在尝试从底图迁移到Cartopy外观的演示示例。我有一个简单的代码,既使用coastlines(),也使用contourf()。我可以分别得到这两个,但不能同时得到。数据集是包含西地中海海面温度数据的netcdf文件。代码是:
import numpy as np
from netCDF4 import Dataset
import cartopy
import matplotlib.pyplot as plt
# DATA
data = Dataset('20190715.0504.n19.nc','r')
lon = data.variables['lon'][:]
lat = data.variables['lat'][:]
sst = data.variables['mcsst'][0,:,:].squeeze()
xxT,yyT = np.meshgrid(lon,lat)
# PLOT
fig = plt.figure(figsize=(10, 5))
ax1 = fig.add_axes([0.01,0.01,0.98,0.98],projection=cartopy.crs.Mercator())
ax1.coastlines()
#ax1.contourf(xxT,yyT,sst)
ax1.set_extent([16.5, -15.0, 35.0, 46.5])
plt.show()使用下面的代码,我得到:

如果我使用:
#ax1.coastlines()
ax1.contourf(xxT,yyT,sst)
ax1.set_extent([16.5, -15.0, 35.0, 46.5])我得到了一个白色的矩形。
如果我使用:
#ax1.coastlines()
ax1.contourf(xxT,yyT,sst)
ax1.set_extent([16.5,-15.0,35.0,46.5],crs=cartopy.crs.Mercator())我得到了等高线数据。

但两者都有:
ax1.coastlines()
ax1.contourf(xxT,yyT,sst)
ax1.set_extent([16.5,-15.0,35.0,46.5],crs=cartopy.crs.Mercator())轮廓是好的!但没有海岸线。如果最终
ax1.coastlines()
ax1.contourf(xxT,yyT,sst)
ax1.set_extent([16.5,-15.0,35.0,46.5])只显示海岸线,而不显示等高线!我试着理解我必须如何继续,因为当我试图将它包含到一个带有显示/隐藏选项的图形用户界面中时,出现了问题。如果我正在使用Python3.7.4,Cartopy0.17,proj4 5.2,matplotlib3.1.1。谢谢!
发布于 2020-02-07 19:19:13
多亏了swatchai的建议,尽管我仍然不明白为什么我需要将transform关键字与特定的PlateCarree投影关键字一起使用,但代码在以下情况下工作得很好:
fig = plt.figure(figsize=(10, 5))
ax1 = fig.add_axes([0.01, 0.01, 0.98, 0.98],projection=cartopy.crs.Mercator())
ax1.coastlines('10m')
ax1.set_extent([16.5, -15.0, 35.0, 46.5])
ax1.contourf(xxT,yyT,sst,transform=cartopy.crs.PlateCarree())下面是结果:

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