我正在尝试对 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()
)
但是,如果将 col_order
或 row_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
范围。仅当 col
或row
可以传递单个列表,并将由适当的变量使用。当两者 col
和row
使用,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/