如何使用Cartopy的单个调色板在同一地图上绘制两个变量?

2024-05-14 04:24:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我被困在这里了。 我的代码:

import os
import matplotlib.pyplot as plt
from netCDF4 import Dataset as netcdf_dataset
import numpy as np

from cartopy import config
import cartopy.crs as ccrs

fname = os.path.join("path", "file")
dataset = netcdf_dataset(fname)

lats = dataset.variables['lat'][:]
lons = dataset.variables['lon'][:]
IVT = dataset.variables['IVT'][0,:,:]
IVTm = dataset.variables['IVTm'][0,:,:]


ax = plt.axes(projection=ccrs.PlateCarree())

map_ivt=ax.contourf(lons, lats, IVT, 60,
             transform=ccrs.PlateCarree())
map_ivt=ax.contourf(lons, lats, IVTm, 60,
             transform=ccrs.PlateCarree())

plt.colorbar(map_ivt, orientation='horizontal')

ax.coastlines()
ax.gridlines()

plt.show()


这是我的结果(我认为我的调色板没有显示正确的值,两个变量的值都超过800):

enter image description here

无法找到在同一地图上绘制“IVT”和“IVTm”的正确方法,如下面的示例所示(IVT为红色,IVTm为蓝色):

IVT is in red, IVTm is in blue

我需要用一个调色板绘制这两个量,就像示例中那样,这个调色板是双向的

多谢各位


Tags: importmapaspltvariablesaxdataset调色板
1条回答
网友
1楼 · 发布于 2024-05-14 04:24:12

为了在对contourf的两个单独调用中实现这一点,您必须为某些值创建一个alpha为0的自定义colormap,这似乎需要大量的工作。如果这些值真的不重叠,这里最简单的方法是将它们组合成一个数组,并用contourf绘图:

IVT_combined = np.where(IVT > 0, IVT, IVTm)

相关问题 更多 >