【问题标题】:Limiting latitudinal extend of a cartopy orthographic projection限制cartopy正投影的纬度延伸
【发布时间】:2019-07-23 08:26:51
【问题描述】:

我正在尝试绘制具有北 (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()

【问题讨论】:

  • 你不能。但也许您只是想在数据周围画一个圆圈?
  • @ImportanceOfBeingErnest 可行。我已经在尝试将网格线放入除 plate-carree 之外的投影中,因此只需禁用轴(或将它们设置为白色/完全透明等)然后绘制网格圆圈和线条将是我唯一的选择吗?
  • 可能的替代方法是继承cartopy.crs.Orthographic 并更改负责在数据周围绘制线条的部分;但我还没有检查你需要为此潜水多深。对于一般情况,这也让人感觉不受欢迎,因为这样你可能会让边框与数据重叠。

标签: python matplotlib map-projections cartopy


【解决方案1】:

(1)。在您的所有绘图中,当您使用scatter() 时,应使用正确的s=value 定义散点的大小,否则使用默认值。我使用 s=0.2,结果图看起来更好。

(2)。对于“中纬度”的情况,您需要使用 set_ylim() 指定正确的 y 限制。这涉及到它们的计算。此处应用transform_point() 的使用。

(3)。对于需要消除不需要的特征的剩余绘图,可以使用适当的圆形剪辑路径。在这两种情况下,它们的周长也用于绘制地图边界。正如我用代码及其输出所演示的那样,它们的存在可能会导致绘制其他地图特征(例如海岸线)时出现问题。

# original is modified and extended
import numpy as np
from matplotlib import pyplot as plt
import cartopy.crs as ccrs
import matplotlib.patches as mpatches  # need it to create clip-path

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

# 'Central latitudes' plot
fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 2, projection=map_proj)
# Note: Limits of plot depends on plotting data, but not exact!
im1 = ax1.scatter(phi[mask_central],
                 theta[mask_central],
                 s = 0.2,
                 c = radii[mask_central],
                 transform=data_crs,
                 vmin = -90,
                 vmax = 90,
                 )
# compute y limits
_, y_btm = map_proj.transform_point(0, -60, ccrs.Geodetic())
_, y_top = map_proj.transform_point(0, 60, ccrs.Geodetic())
# apply y limits
ax1.set_ylim(y_btm, y_top)
ax1.coastlines(color='k', lw=0.35)
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],
             s = 0.1,  # not mandatory
             c = radii[mask_north],
             transform=data_crs,
             vmin = -90,
             vmax = 90,
             )

# use a circular path as map boundary
clip_circle = mpatches.Circle(xy=[0,0], radius=4950000, facecolor='none', edgecolor='k')
ax_N.add_patch(clip_circle)
ax_N.set_boundary(clip_circle.get_path(), transform=None, use_as_clip_path=True)
# with `use_as_clip_path=True` the coastlines do not appear
ax_N.coastlines(color='k', lw=0.75, zorder=13)  # not plotted!
ax_N.set_title('Northern hemisphere1')

# 'Southern hemisphere' plot
ax_S = fig.add_subplot(2, 2, 2, projection=map_proj_S)
ax_S.scatter(phi[mask_south],
             theta[mask_south],
             s = 0.02,
             c = radii[mask_south],
             transform=data_crs,
             vmin = -90,
             vmax = 90,
             )
clip_circle = mpatches.Circle(xy=[0,0], radius=4950000, facecolor='none', edgecolor='k')
ax_S.add_patch(clip_circle)
# applying the clip-circle as boundary, but not use as clip-path 
ax_S.set_boundary(clip_circle.get_path(), transform=None, use_as_clip_path=False)
# with `use_as_clip_path=False` the coastlines is plotted, but goes beyond clip-path
ax_S.coastlines(color='k', lw=0.75, zorder=13)
ax_S.set_title('Southern hemisphere')

# 'Northern hemisphere2' plot, has nice circular limit
fig = plt.figure()
ax = fig.add_subplot(111,projection = map_proj_N)
ax.scatter(phi,
           theta,
           s = 0.2,
           c = radii,
           transform=data_crs,
           vmin = -90,
           vmax = 90,
           )
ax.coastlines(color='k', lw=0.5, zorder=13)
ax.set_title('Northern hemisphere2')
ax.set_global()
plt.show()

输出图:

【讨论】:

  • 我很欣赏这些提示。 (1) 为简洁起见,我忽略了这一点;即使在我的代码中,我也根本没有使用散点图,这很容易演示。我也在火星上绘图,所以海岸线没用。否则,感谢您的提示,图片看起来很棒!
【解决方案2】:

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

【讨论】:

  • 这看起来非常接近我想要的,谢谢!在这种情况下,有没有办法在纬度上定义圆?当极点不在中间时当然没有意义,但在这种情况下,我希望圆圈位于 60N 处,我会在此处切断数据
  • 这与问题要求的完全不同。我会尝试使用网格线来绘制经络。
  • 啊,我没有意识到,我以为一个人可以很容易地抓住范围。我会尝试绘制网格线!
猜你喜欢
  • 1970-01-01
  • 2023-02-20
  • 2015-06-18
  • 2019-02-05
  • 2019-04-11
  • 2017-07-03
  • 2014-03-18
  • 1970-01-01
  • 2017-04-24
相关资源
最近更新 更多