将imshow与底图一起使用:颜色和地图边框不匹配

2024-05-23 21:47:03 发布

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

我正在尝试使用basemap绘制一些grib数据 和imshow而不是contourf。当我用contourf绘制时,我得到了正确的边界和颜色,但当我用imshow绘制时,图像和地图之间存在不匹配。 使用contourfusing contourf绘图。用imshowusing imwshow绘图

我正在使用lcc投影。在文档中,我看到了一些使用圆柱投影的示例,但在这种情况下,我也得到了不匹配。 相关代码如下

m = Basemap(llcrnrlon=-55, llcrnrlat=55.8, urcrnrlon=80, urcrnrlat=80, lat_1=72, lat_0=72., lon_0=-36, resolution='h', projection='lcc')
x, y = m(lons, lats)

data = data - 273.15
import matplotlib as mpl
cmap = mpl.cm.RdBu_r
norm = mpl.colors.Normalize(vmin=-55, vmax=15)
minVal = min(data.flatten())
maxVal = max(data.flatten())
clev = np.arange(minVal,maxVal,0.01)
#using contourf
CS_tmp = m.contourf(x,y,data,clev,cmap=plt.cm.coolwarm)
#using imshow
CS_tmp = m.imshow(data2,cmap=cmap) #,extent=extent)

我的问题是,我应该应用什么样的转换 对数据有什么看法?我试着学习这个教程 https://basemaptutorial.readthedocs.io/en/latest/utilities.html#interp 它引用了transform_scalar,它只起作用 用于圆柱形投影

建议对非圆柱体投影使用interp 当输入矩阵在经纬度上不规则(即不是圆柱投影)时,此方法无法正确使用,因为经纬度网格将不规则。有关解决方案,请参阅interp示例

我试着效仿这个例子,但没有成功 我仍然看到不匹配

x2 = np.linspace(x[0][0],x[0][-1],x.shape[1]*2)
y2 = np.linspace(y[0][0],y[-1][0],y.shape[0]*2)
x2, y2 = np.meshgrid(x2, y2)
data_trans = interp(data,  x[0], y[:, 0], x2, y2,order=1)

谁能告诉我这里的适当转变是什么

谢谢你的帮助

卡洛斯


Tags: 数据绘图datanp绘制mplcmap投影
1条回答
网友
1楼 · 发布于 2024-05-23 21:47:03

我认为问题在于imshow绘图适合于绘图中的整个图形窗口,因为matplotlib无法从数组中获取任何坐标信息

如果使用另一个投影,则可以正确设置边界,但使用基于极坐标的投影时,坐标的范围在纬度和经度上会上升到80°

因此,您必须将nan添加到data2数组中

代码可能如下所示:

# take a raw array of zeros along longitude axes 
base_nan_array = np.zeros(len(x))

# find out how many rows must be added
n_lat_rows =len(y) - data2.shape[1]

# fill up nan array 
nan_array = np.array([base_nan_array,]*n_lat_rows)

# append to data (you have to find the direction of append on your own bc. no test data is avail.)
np.append([data2, nan_array], axis=0)

这将使阵列覆盖整个底图

相关问题 更多 >