在 seaborn facetgrid 的各个方面设置轴限制

set axis limits on individual facets of seaborn facetgrid

我正在尝试为 Seaborn facetgrid distplot 的每个方面将 x 轴限制设置为不同的值。我知道我可以通过 g.axes 访问子图中的所有轴,所以我尝试迭代它们并将 xlim 设置为:

g = sns.FacetGrid(
    mapping,
    col=options.facetCol,
    row=options.facetRow,
    col_order=sorted(cols),
    hue=options.group,
)
g = g.map(sns.distplot, options.axis)

for i, ax in enumerate(g.axes.flat):  # set every-other axis for testing purposes
    if i % 2 == 0[enter link description here][1]:
        ax.set_xlim(-400, 500)
    else:
        ax.set_xlim(-200, 200)

但是,当我这样做时,所有轴都设置为 (-200, 200),而不仅仅是其他每个面。

我做错了什么?

mwaskom 有解决方案;为了完整性而在此处发布 - 只需将以下行更改为:

g = sns.FacetGrid(
    mapping,
    col=options.facetCol,
    row=options.facetRow,
    col_order=sorted(cols),
    hue=options.group,
    sharex=False,  # <- This option solved the problem!
)

根据 mwaskom you can simply use FacetGridsharex(分别为 sharey)的建议,允许绘图具有独立的轴刻度:

share{x,y} : bool, ‘col’, or ‘row’ optional

If true, the facets will share y axes across columns and/or x axes across rows.

例如:

  • sharex=False 每个地块都有自己的轴
  • sharex='col'每一列都有自己的轴
  • sharex='row'每一行都有自己的轴(即使这个对我来说意义不大)
sns.FacetGrid(data, ..., sharex='col')

如果您间接使用 FacetGrid,例如通过 displot or relplot,您将必须使用 facet_kws 关键字参数:

sns.displot(data, ..., facet_kws={'sharex': 'col'})