python - 使用 Seaborn 对象接口(interface)时如何对构面进行排序

标签 python seaborn seaborn-0.12.x

我正在尝试对 seaborn objects interface 生成的图中的各个方面进行排序.

import numpy as np
import seaborn as sns
import seaborn.objects as so
import matplotlib.pyplot as plt

df = sns.load_dataset("iris")
df["species"] = df["species"].astype("category")
df["species"] = df["species"].cat.codes
rng = np.random.default_rng(seed=0)
df["subset"] = rng.choice(['A','B','C'], len(df), replace=True) 

fig = plt.figure(figsize=(6.4 * 2.0, 4.8 * 2.0))

_ = (
    so.Plot(df, x="sepal_length", y="sepal_width")
    .facet(row="species", col="subset")
    .add(so.Dot())
    .on(fig)
    .plot()
)

A plot with facets row-wise and column-wise. The columns are, left to right--C, B, A--and the rows are, top to bottom--0, 1, 2.

但是,如果将 col_orderrow_order 作为参数传递给 .facet() 行,则会出现“意外的关键字参数”引发 TypeError

_ = (
    so.Plot(df, x="sepal_length", y="sepal_width")
    .facet(
        row="species",
        col="subset",
        row_order=['A','C','B'],
        col_order=[0,2,1]
    )
    .add(so.Dot())
    .on(fig)
    .plot()
)
TypeError: Plot.facet() got an unexpected keyword argument 'row_order'

使用seaborn.objects接口(interface)时,facet应该如何排序?

请注意,这个问题与"Seaborn ordering of facets"非常相似。当使用 seaborn 而不是 seaborn.objects 模块生成绘图时,这是同样的问题。

理想情况下,在 seaborn.objects 接口(interface)中使用 facet()wrap 参数时,答案也应该有效。

最佳答案

Plot.facet 有一个order范围。仅当 colrow可以传递单个列表,并将由适当的变量使用。当两者 colrow使用,order可以是一本字典 col/row键:

(
    so.Plot(df, x="sepal_length", y="sepal_width")
    .facet(row="species", col="subset", order={"row": [2, 1, 0], "col": ["A", "B", "C"]})
    .add(so.Dot())
    .layout(size=(6.4 * 2, 4.8 * 2))
)

关于python - 使用 Seaborn 对象接口(interface)时如何对构面进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77019628/

相关文章:

Python - 将元组列表水平写入文本文件

python - 如何用点而不是条形图创建直方图

python - 如何在 seaborn 中获得 2 个独立的地 block ?

python - 如何在seaborn对象api中定义带有rgb值的颜色?

python - 图例使用 Seaborn Objects API 模糊绘图

python - 如何将图形图例重新定位到子图形中

python - 查找某个单词的同义词会产生 WordNetError

python - 使用 cronjob 运行带有参数的 python 脚本会出现错误 :/bin/sh: password: command not found

python - 如何使用python检测进程是否正在运行

python - 如何将图例移动到 seaborn 散点图之外?