为什么 subplot_kws 投影隐藏纬度和经度?

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

使用 xarray 数据集,如何将 FacetGrid 与特定的 cartopy 投影一起使用并沿轴保留纬度和经度值?

我尝试按照用户指南中所述使用分面。一旦我尝试为子图提供特定的投影,我就会丢失轴上的纬度和经度值

这是一个例子:

import xarray as xr
import cartopy.crs as ccrs

airtemps = xr.tutorial.open_dataset("air_temperature")
air = airtemps.air - 273.15
t = air.isel(time=slice(0, 365 * 4, 250) )
g_simple = t.plot.contourf(x="lon", y="lat", col="time", col_wrap=3,
                           transform = ccrs.PlateCarree(),
                           subplot_kws={"projection": ccrs.PlateCarree()},
                           levels=51,
)

但是如果我注释掉投影线

import xarray as xr
import cartopy.crs as ccrs

airtemps = xr.tutorial.open_dataset("air_temperature")
air = airtemps.air - 273.15
t = air.isel(time=slice(0, 365 * 4, 250) )
g_simple = t.plot.contourf(x="lon", y="lat", col="time", col_wrap=3,
                           #  subplot_kws={"projection": ccrs.PlateCarree()},
                           levels=51,
)

为什么会发生这种情况?我该如何解决这个问题?

matplotlib python-xarray cartopy facet-grid
1个回答
0
投票

Cartopy 默认关闭轴标签,因为它们对于 Cartopy 的所有投影都没有意义。您可以在每个轴上使用

set_visible
方法再次打开它们。我还在这里添加了海岸线,因为我认为这有助于让我们清楚地看到我们正在寻找的地方。

xarray 似乎在某处使用了

tight_layout
。我不知道如何为标签腾出空间并使用
tight_layout
将颜色条保持在正确的位置,因此我改用更现代的压缩布局。事实证明,如果您已经有颜色条,则无法以这种方式切换布局引擎,因此我最初在调用
contour
时关闭了颜色条,并在切换布局引擎后添加回来。

import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

airtemps = xr.tutorial.open_dataset("air_temperature")
air = airtemps.air - 273.15
t = air.isel(time=slice(0, 365 * 4, 250) )
g_simple = t.plot.contourf(x="lon", y="lat", col="time", col_wrap=3,
                           transform = ccrs.PlateCarree(),
                           subplot_kws={"projection": ccrs.PlateCarree()},
                           levels=51, add_colorbar=False
)

for ax in g_simple.fig.axes:  # loop through the map axes
    subplotspec = ax.get_subplotspec()
    if subplotspec.is_last_row():
        ax.xaxis.set_visible(True)
    if subplotspec.is_first_col():
        ax.yaxis.set_visible(True)
    ax.coastlines()
    
g_simple.fig.set_layout_engine("compressed")
g_simple.fig.get_layout_engine().set(h_pad=0.2)  # More vertical space between maps
g_simple.add_colorbar()

plt.show()

[StackOverflow 上的图像上传目前似乎已中断]

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