如何控制 xarray 中 facet gird 行 and/or 列的顺序?

How to control the order of facet gird rows and/or columns in xarray?

我正在尝试更改用于在 xarray 中制作小平面网格的变量的顺序。例如,我有 [a,b,c,d] 作为列名。我想将其重新排序为 [c,d,a,b]。不幸的是,与 seaborn 不同,我在 xarray plot 函数中找不到 col_order 或 row_order 等参数(

https://xarray.pydata.org/en/stable/generated/xarray.plot.FacetGrid.html

更新: 为了帮助自己更好地解释我需要什么,我从 xarray 的用户指南中获取了以下示例: 在下面的示例中,我需要更改月份的位置。我的意思是,例如,我想将第 7 个月作为第一列,将第 2 个月作为第 5 列,依此类推。

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
ds = xr.tutorial.open_dataset("air_temperature.nc").rename({"air": "Tair"})

# we will add a gradient field with appropriate attributes
ds["dTdx"] = ds.Tair.differentiate("lon") / 110e3 / np.cos(ds.lat * np.pi / 180)
ds["dTdy"] = ds.Tair.differentiate("lat") / 105e3
ds.dTdx.attrs = {"long_name": "$∂T/∂x$", "units": "°C/m"}
ds.dTdy.attrs = {"long_name": "$∂T/∂y$", "units": "°C/m"}
monthly_means = ds.groupby("time.month").mean()
# xarray's groupby reductions drop attributes. Let's assign them back so we get nice labels.
monthly_means.Tair.attrs = ds.Tair.attrs
fg = monthly_means.Tair.plot(
    col="month",
    col_wrap=4,  # each row has a maximum of 4 columns
)
plt.show()

非常感谢任何帮助。

xarray 会尊重数据的形状,因此您可以在绘图之前重新排列数据:

In [2]: ds = xr.tutorial.open_dataset("air_temperature.nc")

In [3]: ds_mon = ds.groupby("time.month").mean()

In [4]: # order the data by month, descending
   ...: ds_mon.air.sel(month=list(range(12, 0, -1))).plot(
   ...:     col="month", col_wrap=4,
   ...: )

Out[4]: <xarray.plot.facetgrid.FacetGrid at 0x16b9a7700>