Cartopy无法正确轮廓旋转网格上的数据

问题描述 投票:0回答:1

当轮廓在旋转的极点网格上定义的数据集时,我得到以下结果:

enter image description here

这只是使用contourcontourf时的问题,而不是pcolormesh

任何人都知道这里会发生什么?我应该提交错误报告,如果是,我应该使用cartopy还是使用matplotlib?

再现

cartopy
1个回答
1
投票

轮廓数据应分为两部分,以避免发现错误。我选择longitude=0作为分界线。 Numpy的掩码数组技术用于实现数据操作。这是生成有用图的工作代码。

import numpy as np
import cartopy.crs as ccrs
import matplotlib as mpl
import matplotlib.colors as colors
import matplotlib.pyplot as plt
import numpy.ma as ma
from netCDF4 import Dataset

nc = Dataset('./data/snow_rlat_rlon.nc')

# Prep values for contouring
snow_2d_array = nc.variables[u'snowfall'][:]   # need *(86400*365); mm/s-> mm/yr
lat_2d_array = nc.variables[u'lat2d'][:]
lon_2d_array = nc.variables[u'lon2d'][:]

# do masked-array on the lon_2d
lon2d_greater = ma.masked_greater(lon_2d_array, -0.01)
lon2d_lesser = ma.masked_less(lon_2d_array, 0)

# apply masks to other associate arrays: lat_2d
lat2d_greater = ma.MaskedArray(lat_2d_array, mask=lon2d_greater.mask)
lat2d_lesser = ma.MaskedArray(lat_2d_array, mask=lon2d_lesser.mask)
# apply masks to other associate arrays: snow_2d
snow_2d_greater = ma.MaskedArray(snow_2d_array, mask=lon2d_greater.mask)
snow_2d_lesser = ma.MaskedArray(snow_2d_array, mask=lon2d_lesser.mask)

# set levels for contouring of snow_2d
levels = (0, 25, 50, 75, 100, 200, 400, 600, 800, 1000, 2000, 4000)

# get snow_2d value-limits for use with colormap
vmax, vmin = snow_2d_array.max()*86400*365, snow_2d_array.min()*86400*365
cmap1 = "viridis"
norm1 = colors.BoundaryNorm(boundaries=levels, ncolors=16)
norm2 = colors.Normalize(vmin=vmin, vmax=vmax/4)

# setup fig+axes, specifying projection to use
fig, ax = plt.subplots(subplot_kw={'projection': ccrs.SouthPolarStereo()})
fig.set_size_inches([10, 10])

ax.coastlines(color="red", linewidth=2)  # draw coastlines in red

# plot contour using each part of the 2 masked data sets
ct1 = ax.contour(lon2d_greater, lat2d_greater, snow_2d_greater*86400*365, \
                 norm=norm2, levels=levels, \
                 transform=ccrs.PlateCarree())

ct2 = ax.contour(lon2d_lesser, lat2d_lesser, snow_2d_lesser*86400*365, \
                 norm=norm2, levels=levels, \
                 transform=ccrs.PlateCarree()) 

#plt.colorbar(ct1, shrink=0.85)
plt.show()

输出图:

spole_plot

对于填充轮廓,用ax.contour()替换ax.contourf()并添加:

ax.set_xlim((-4052327.4304452268, 4024164.250636036))

plt.show()面前。

contourf

希望它对您的项目有用。

© www.soinside.com 2019 - 2024. All rights reserved.