简体   繁体   English

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

[英]Mask out Islands and contour colors outside Greenland (Python)

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 )数据已存储( https://drive.google.com/drive/folders/10XvLZPC9vX9odkfU6aV6qBIxRe9IQvyZ?usp=sharing

This is a picture of the output of my code:这是我的代码输出的图片:

在此处输入图像描述

Any help will be much appreciated.任何帮助都感激不尽。

you could use shapely.vectorized.contains to mask the points within the shape:您可以使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM