掩盖格陵兰岛(Python)以外的岛屿和轮廓颜色

发布于 2025-01-31 03:30:02 字数 2749 浏览 3 评论 0 原文

我在下面有这个代码,该代码绘制了格陵兰岛的近空气表面温度,但是我需要掩盖格陵兰岛外的颜色以及格陵兰岛主要岛屿以外的岛​​屿。

以下是我的代码:

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)

数据存储( htttpps e> com/drive/drive/drive/10xvlzpc9vx9vx9odkfu6qbixre9iqvyz?usp =共享

这是我代码输出的图片:

“在此处输入图像说明”

任何帮助都将不胜感激。

I have this code below that plots the near air surface temperature of greenland, but, I need to mask out the colors outside Greenland and also the islands outside the main island of Greenland.

Below is my code:

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)

The data is stored (https://drive.google.com/drive/folders/10XvLZPC9vX9odkfU6aV6qBIxRe9IQvyZ?usp=sharing)

This is a picture of the output of my code:

enter image description here

Any help will be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2025-02-07 03:30:03

you could use shapely.vectorized.contains< /code> 掩盖形状内的点:

# 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)

you could use shapely.vectorized.contains to mask the points within the shape:

# 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文