gpt4 book ai didi

python - 屏蔽格陵兰岛以外的岛屿和轮廓颜色(Python)

转载 作者:行者123 更新时间:2023-12-05 04:29:23 25 4
gpt4 key购买 nike

下面的代码绘制了格陵兰岛的近空气表面温度,但是,我需要屏蔽掉格陵兰岛以外的颜色以及格陵兰岛主岛以外的岛屿。

下面是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.mpl.ticker as cmt
import xarray as xr
import matplotlib.ticker as ticker


# Plot func.
def PlotMap(lon,lat,var1,var2,var3,levs, mapbar,unidades, titulo, figura):
parameters = {'xtick.labelsize':14,
'ytick.labelsize':14,
'axes.labelsize':14,
'boxplot.whiskerprops.linewidth':4,
'boxplot.boxprops.linewidth': 4,
'boxplot.capprops.linewidth':4,
'boxplot.medianprops.linewidth':4,
'axes.linewidth':1.5}
plt.rcParams.update(parameters)
fig, ax = plt.subplots(figsize=(9,10))
ax=plt.subplot(1,1,1)


im=ax.contourf(lon,lat,var1,levs, cmap = mapbar)
ax.contour(lon,lat,var2,0,colors='black')
ax.contour(lon,lat,var3,0,colors='red')
#ax.contour(lon,lat,var3,0,colors='blue')
fig.colorbar(im,orientation = 'vertical',shrink=0.8, label = unidades, pad=0.05)
fig.suptitle(titulo, fontsize='large',weight='bold')
plt.tight_layout()
fig.savefig(figur, bbox_inches='tight',pad_inches = 0, dpi=400);

# Open file
grl = xr.open_dataset('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Trabajo fin de master/OUTPUTS/output/ens1/0/yelmo2d.nc')
topo = xr.open_dataset('/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/Trabajo fin de master/Greenland/GRL-16KM/GRL-16KM_TOPO-M17.nc')


xc = grl['xc'].data
yc = grl['yc'].data
smb = grl['T_srf'].mean(dim="time")
border = grl['z_srf'].mean(dim="time")
border_mask = grl['z_srf'].mean(dim="time").data
region_mask = topo['mask'].data
new_smb = smb.where(region_mask>1,np.nan)
new_border = border.where(border_mask<1290,np.nan)

#bord = grl['regions']
#bord_mask = grl['regions'].data
#new_border2 = bord.where(bord_mask>1.3,np.nan)

#cmin=np.min(zbed)
#cmax=np.max(zbed)+10
cmin=np.min(smb)
cmax= np.max(smb) + 10
levels=np.arange(cmin,cmax,10)
mapbar='bwr'
titulo='Greenland Temp'
unidades='K'
figur='/Users/jacobgarcia/Desktop/Master en Meteorologia/TFM/figs/smb.png'
PlotMap(xc,yc,smb,new_border,smb,levels,mapbar,unidades, titulo, figur)

数据存储(https://drive.google.com/drive/folders/10XvLZPC9vX9odkfU6aV6qBIxRe9IQvyZ?usp=sharing)

这是我的代码输出的图片:

enter image description here

任何帮助将不胜感激。

最佳答案

你可以使用 shapely.vectorized.contains掩盖形状内的点:

# build 2d arrays of x and y the same shape as your data
yy, xx = np.meshgrid(yc, xc)

# use a single shapely polygon or MultiPolygon
contained = shapely.vectorized.contains(polygon, xx, yy)
in_shape = xr.DataArray(
contained, dims=['yc', 'xc'], coords=[yc, xc]
)

smb_masked = smb.where(in_shape)

关于python - 屏蔽格陵兰岛以外的岛屿和轮廓颜色(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72332129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com