使用 cartopy 和 AxesGrid 时如何获得 "L" 形状的刻度标签
How to get "L" shaped ticklabels when using cartopy and AxesGrid
(从this Github issue复制过来)
我正在尝试按照 cartopy 文档 here 中显示的示例修改使用 cartopy + AxesGrid 的 Axes 的标记标签。但与示例不同的是,我只想要 "L" 形状的刻度标签,即最左边一列的垂直刻度标签和底行的水平刻度标签。
当我编辑任何轴的水平刻度标签时,该列中所有轴的水平刻度标签都会随之更改。同样对于垂直刻度标签和该行中的所有轴。 (无论是否使用 cartopy,AxesGrid 都是如此,但在后一种情况下,我可以设置 label_mode='L' 而不必费心手动更改它们。)
我在 this gist 中创建了一个 MWE,转载于此:
import cartopy.crs as ccrs
from cartopy.mpl.geoaxes import GeoAxes
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import AxesGrid
fig = plt.figure()
projection = ccrs.PlateCarree()
axes_class = (GeoAxes,
dict(map_projection=projection))
axgrid = AxesGrid(fig, 111, axes_class=axes_class,
nrows_ncols=(2, 2),
axes_pad=0.3,
label_mode='')
for n, ax in enumerate(axgrid):
ax.set_xticks(np.linspace(-180, 180, 5), crs=projection)
ax.set_yticks(np.linspace(-90, 90, 5), crs=projection)
# Removing the xticklabels from the upper right corner panel
# also removes them from the lower right corner panel
axgrid[1].set_xticklabels([])
解决方案,感谢 @spencerkclark, is available in this gist 并在此处复制。
fig = plt.figure()
projection = ccrs.PlateCarree()
axes_class = (GeoAxes,
dict(map_projection=projection))
axgrid = AxesGrid(fig, 111, axes_class=axes_class,
nrows_ncols=(2, 2),
axes_pad=0.3,
label_mode='')
for n, ax in enumerate(axgrid):
ax.set_xlim(-180, 180)
ax.set_ylim(-90, 90)
ax.set_xticks(np.linspace(-180, 180, 5), crs=projection)
ax.set_yticks(np.linspace(-90, 90, 5), crs=projection)
# Make ticklabels on inner axes invisible
axes = np.reshape(axgrid, axgrid.get_geometry())
for ax in axes[:-1, :].flatten():
ax.xaxis.set_tick_params(which='both',
labelbottom=False, labeltop=False)
for ax in axes[:, 1:].flatten():
ax.yaxis.set_tick_params(which='both',
labelbottom=False, labeltop=False)
编辑:如果面板数量小于 nrows*ncols,则此逻辑失败,例如如果它是 4x2 网格但只有 7 个面板。以下也适用于这些情况:
def _hide_inner_ax_ticklabels(axgrid):
"""Produce 'L' shaped ticklabels: left row and bottom column only.
Must do this manually when using axgrid + cartopy due to bug.
See https://github.com/SciTools/cartopy/issues/939.
"""
total_panels = axgrid._nrows * axgrid._ncols
blank_panels = total_panels - axgrid.ngrids
newgrid = np.concatenate([axgrid, [None]*blank_panels])
axes = np.reshape(newgrid, axgrid.get_geometry())
for ax in axes[:-1, :].flatten():
try:
ax.xaxis.set_tick_params(which='both', labelbottom=False,
labeltop=False)
except AttributeError:
pass
for ax in axes[:, 1:].flatten():
try:
ax.yaxis.set_tick_params(which='both', labelbottom=False,
labeltop=False)
except AttributeError:
pass
(从this Github issue复制过来)
我正在尝试按照 cartopy 文档 here 中显示的示例修改使用 cartopy + AxesGrid 的 Axes 的标记标签。但与示例不同的是,我只想要 "L" 形状的刻度标签,即最左边一列的垂直刻度标签和底行的水平刻度标签。
当我编辑任何轴的水平刻度标签时,该列中所有轴的水平刻度标签都会随之更改。同样对于垂直刻度标签和该行中的所有轴。 (无论是否使用 cartopy,AxesGrid 都是如此,但在后一种情况下,我可以设置 label_mode='L' 而不必费心手动更改它们。)
我在 this gist 中创建了一个 MWE,转载于此:
import cartopy.crs as ccrs
from cartopy.mpl.geoaxes import GeoAxes
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import AxesGrid
fig = plt.figure()
projection = ccrs.PlateCarree()
axes_class = (GeoAxes,
dict(map_projection=projection))
axgrid = AxesGrid(fig, 111, axes_class=axes_class,
nrows_ncols=(2, 2),
axes_pad=0.3,
label_mode='')
for n, ax in enumerate(axgrid):
ax.set_xticks(np.linspace(-180, 180, 5), crs=projection)
ax.set_yticks(np.linspace(-90, 90, 5), crs=projection)
# Removing the xticklabels from the upper right corner panel
# also removes them from the lower right corner panel
axgrid[1].set_xticklabels([])
解决方案,感谢 @spencerkclark, is available in this gist 并在此处复制。
fig = plt.figure()
projection = ccrs.PlateCarree()
axes_class = (GeoAxes,
dict(map_projection=projection))
axgrid = AxesGrid(fig, 111, axes_class=axes_class,
nrows_ncols=(2, 2),
axes_pad=0.3,
label_mode='')
for n, ax in enumerate(axgrid):
ax.set_xlim(-180, 180)
ax.set_ylim(-90, 90)
ax.set_xticks(np.linspace(-180, 180, 5), crs=projection)
ax.set_yticks(np.linspace(-90, 90, 5), crs=projection)
# Make ticklabels on inner axes invisible
axes = np.reshape(axgrid, axgrid.get_geometry())
for ax in axes[:-1, :].flatten():
ax.xaxis.set_tick_params(which='both',
labelbottom=False, labeltop=False)
for ax in axes[:, 1:].flatten():
ax.yaxis.set_tick_params(which='both',
labelbottom=False, labeltop=False)
编辑:如果面板数量小于 nrows*ncols,则此逻辑失败,例如如果它是 4x2 网格但只有 7 个面板。以下也适用于这些情况:
def _hide_inner_ax_ticklabels(axgrid):
"""Produce 'L' shaped ticklabels: left row and bottom column only.
Must do this manually when using axgrid + cartopy due to bug.
See https://github.com/SciTools/cartopy/issues/939.
"""
total_panels = axgrid._nrows * axgrid._ncols
blank_panels = total_panels - axgrid.ngrids
newgrid = np.concatenate([axgrid, [None]*blank_panels])
axes = np.reshape(newgrid, axgrid.get_geometry())
for ax in axes[:-1, :].flatten():
try:
ax.xaxis.set_tick_params(which='both', labelbottom=False,
labeltop=False)
except AttributeError:
pass
for ax in axes[:, 1:].flatten():
try:
ax.yaxis.set_tick_params(which='both', labelbottom=False,
labeltop=False)
except AttributeError:
pass