[英]Lack of Projection for Cartopy Contour
I'm trying to put some data onto a contourmap via cartopy. 我正在尝试通过Cartopy将一些数据放到轮廓图上。 However, after plotting the data, the projection still seems to be off.
但是,在绘制数据后,投影似乎仍然不正确。 The surface_temp.X and surface_temp.Y are lat/lon, while masked_fill is the actual data values.
surface_temp.X和surface_temp.Y为经/纬度,而masked_fill为实际数据值。 This seems to have worked in basemap, but I'm not sure why it doesn't in cartopy.
这似乎已经在底图中起作用了,但是我不确定为什么它不在cartopy中。
Cartopy: Cartopy:
fig = plt.figure(figsize=(12,4.76), dpi=100)
fig.clf()
ax = plt.axes(projection=ccrs.Mercator())
ax.coastlines()
ax.contourf(surface_temp.X, surface_temp.Y, surface_temp.masked_fill[:], latlon = 'true', transform = ccrs.Mercator())
plt.show()
Basemap: 底图:
fig = plt.figure(figsize=(15,4.76), dpi=100)
fig.clf()
plt.axes([0,0,1,1], frameon=False)
plt.title(title)
m = Basemap(projection='merc',llcrnrlat=-80,urcrnrlat=80, llcrnrlon=0,urcrnrlon=360,lat_ts=20,resolution='c')
m.contourf(surface_temp.X, surface_temp.Y, surface_temp.masked_fill[:], latlon = 'true')
Basemap Result: 底图结果:
Cartopy Result (Contour commented out): Cartopy结果(轮廓被注释掉):
Cartopoy Result (Contour) Cartopoy结果(轮廓)
The paradigm of cartopy seems to be to always work on lat/lon coordinates. Cartopy的范例似乎总是在纬度/经度坐标上起作用。 This means, you should not transform your data according to the projection, but stay in lat/lon.
这意味着,您不应根据预测来变换数据,而应保持经/纬度。
Hence, instead of 因此,代替
ax.contourf(..., transform = ccrs.Mercator())
you would need 你需要
ax.contourf(..., transform = ccrs.PlateCarree())
A complete example: 一个完整的例子:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.examples.waves import sample_data
ax = plt.axes(projection=ccrs.Mercator())
lons, lats, data = sample_data(shape=(20, 40))
ax.contourf(lons, lats, data, transform=ccrs.PlateCarree())
ax.coastlines()
ax.gridlines()
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.