Cartopy/matplotlib:contourf干扰设置\u边界

2024-06-28 11:57:22 发布

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

我使用matplotlib、cartopy和albersqualarea投影手动设置等高线图的边界。使用this code,我将自定义边界设置为仅包含包含数据的区域。使用下面显示的代码,它似乎工作得很好(第一个图)。然而,当我将低纬度边界设置为北纬38度或更高时,边界不再设置(第二个图)。你知道吗

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import matplotlib.path as mpath

# the latitude and longitude boundaries
latmin = 40
latmax = 75
lonmin = 230
lonmax = 310

lats = np.linspace(latmax, latmin, latmax - latmin + 1)
lons = np.linspace(lonmin, lonmax, lonmax - lonmin + 1)

xs, ys = np.meshgrid(lons, lats)

# the data
z = np.zeros_like(xs)
data = np.random.randn(*z.shape)

central_lat = 37.5
central_lon = -90

# the axes (using the AlbersEqualArea projection)
ax = plt.axes(projection=ccrs.AlbersEqualArea(central_longitude=central_lon, central_latitude=central_lat))

ax.coastlines()

# plot the data
ax.contourf(xs, ys, data, levels=24, transform=ccrs.PlateCarree())

# adjust the boundary
vertices = [(lon, latmin) for lon in range(lonmin, lonmax + 1, 1)] + \
    [(lon, latmax) for lon in range(lonmax, lonmin - 1, -1)]
boundary = mpath.Path(vertices)
ax.set_boundary(boundary, transform=ccrs.PlateCarree())

plt.show()

我遇到这个问题,只有当我同时使用轮廓和设置边界。在使用pcolormesh和设置\u边界时也会发生这种情况。因此,我怀疑contourf和Setu边界之间有干扰。我尝试切换这些命令的顺序(例如,在绘制数据之前设置边界),但问题仍然存在。你知道吗

有解决办法吗,还是我遗漏了什么?你知道吗

Figure 1, with boundaries

Figure 2, boundaries not set correctly


Tags: theimportdataasnpax边界central