首页 > 解决方案 > 限制cartopy正投影的纬度延伸

问题描述

我正在尝试绘制具有北(0-40N)和南(0-40S)半球的正交投影以及中纬度(60N-60S)的Mollweide投影的球体地图。我得到以下情节:

在此处输入图像描述

这显示了一个问题:在半球形图周围有一个带有切角的方形边界框。请注意,所有三个图的颜色范围都相同(-90 到 90)。

然而,当我在不限制其范围的情况下绘制一个半球时,我得到一个圆形边界框,正如正交投影所预期的那样:

在此处输入图像描述

使用plt.xlim(-90,-50)会导致垂直条纹和plt.ylim(-90,-50)水平条纹,所以这也不是解决方案。

如何在保持圆形边界框的同时限制正交投影的纬度范围?

生成上述图表的代码:

import numpy as np
from matplotlib import pyplot as plt
import cartopy.crs as ccrs

# Create dummy data, latitude from -90(S) to 90 (N), lon from -180 to 180
theta, phi = np.meshgrid(np.arange(0,180),np.arange(0,360));
theta = -1*(theta.ravel()-90)
phi = phi.ravel()-180
radii = theta

# Make masks for hemispheres and central
mask_central = np.abs(theta) < 60
mask_north = theta > 40
mask_south = theta < -40

data_crs= ccrs.PlateCarree()  # Data CRS
# Grab map projections for various plots
map_proj = ccrs.Mollweide(central_longitude=0)
map_proj_N = ccrs.Orthographic(central_longitude=0, central_latitude=90)
map_proj_S = ccrs.Orthographic(central_longitude=0, central_latitude=-90)

fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 2,projection=map_proj)
im1 = ax1.scatter(phi[mask_central],
                 theta[mask_central],
                 c = radii[mask_central],
                 transform=data_crs,
                 vmin = -90,
                 vmax = 90,
                 )
ax1.set_title('Central latitudes')

ax_N = fig.add_subplot(2, 2, 1, projection=map_proj_N)
ax_N.scatter(phi[mask_north],
             theta[mask_north],
             c = radii[mask_north],
             transform=data_crs,
             vmin = -90,
             vmax = 90,
             )
ax_N.set_title('Northern hemisphere')

ax_S = fig.add_subplot(2, 2, 2, projection=map_proj_S)
ax_S.scatter(phi[mask_south],
             theta[mask_south],
             c = radii[mask_south],
             transform=data_crs,
             vmin = -90,
             vmax = 90,
             )
ax_S.set_title('Southern hemisphere')

fig = plt.figure()
ax = fig.add_subplot(111,projection = map_proj_N)
ax.scatter(phi,
           theta,
           c = radii,
           transform=data_crs,
           vmin = -90,
           vmax = 90,
           )
ax.set_title('Northern hemisphere')
plt.show()

标签: pythonmatplotlibmap-projectionscartopy

解决方案


matplotlib 中通常的轴是矩形的。然而,对于 cartopy 中的某些投影,显示一个矩形的一部分甚至没有定义是没有意义的。这些区域被包围了。这样可以确保轴内容始终保持在边界内。

如果您不希望这样做,而是使用圆形边框,即使部分绘图可能位于圆圈之外,您也可以手动定义该圆圈:

import numpy as np
from matplotlib import pyplot as plt
import cartopy.crs as ccrs

# Create dummy data, latitude from -90(S) to 90 (N), lon from -180 to 180
theta, phi = np.meshgrid(np.arange(0,180),np.arange(0,360));
theta = -1*(theta.ravel()-90)
phi = phi.ravel()-180
# Make mask for hemisphere
mask_north = theta > 40
data_crs= ccrs.PlateCarree()  # Data CRS
# Grab map projections for various plots
map_proj_N = ccrs.Orthographic(central_longitude=0, central_latitude=90)


fig = plt.figure()
ax_N = fig.add_subplot(121, projection=map_proj_N)
ax_N.scatter(phi[mask_north], theta[mask_north],
             c = theta[mask_north], transform=data_crs,
             vmin = -90, vmax = 90)
ax_N.set_title('Northern hemisphere')

### Remove undesired patch
ax_N.patches[0].remove()
### Create new circle around the axes:
circ = plt.Circle((.5,.5), .5, edgecolor="k", facecolor="none",
                  transform=ax_N.transAxes, clip_on=False)
ax_N.add_patch(circ)



#### For comparisson, plot the full data in the right subplot:
ax = fig.add_subplot(122,projection = map_proj_N)
ax.scatter(phi, theta, c = theta,
           transform=data_crs, vmin = -90, vmax = 90)
ax.set_title('Northern hemisphere')
plt.show()

在此处输入图像描述


推荐阅读