Python中cartopy在同一个fig中同时画海温和风速
时间: 2023-06-10 08:04:10 浏览: 198
可以通过以下代码实现:
```python
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np
#生成数据
lons, lats = np.meshgrid(np.linspace(-180, 180, 100), np.linspace(-90, 90, 100))
sea_surface_temp = np.sin(np.radians(lats))**10 + np.cos(4*np.radians(lats)) * np.cos(4*np.radians(lons))
u_wind = 10 * np.cos(np.radians(lats)) * np.sin(np.radians(lons))
v_wind = -10 * np.sin(np.radians(lats))
#画图
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(1, 2, 1, projection=ccrs.PlateCarree())
ax.set_global()
ax.coastlines()
ax.add_feature(cfeature.LAND)
im = ax.contourf(lons, lats, sea_surface_temp, transform=ccrs.PlateCarree())
cb = fig.colorbar(im)
ax2 = fig.add_subplot(1, 2, 2, projection=ccrs.PlateCarree())
ax2.set_global()
ax2.coastlines()
ax2.add_feature(cfeature.LAND)
ax2.quiver(lons, lats, u_wind, v_wind, transform=ccrs.PlateCarree())
plt.show()
```
其中,左侧的子图绘制了海温数据,使用`ax.contourf()`函数画色彩填充图,右侧的子图则绘制了风速数据,使用`ax.quiver()`函数画箭头图。两者同时在同一个fig中展示。注意,要先设置`projection=ccrs.PlateCarree()`来指定投影方式。
阅读全文