有没有办法在 seaborn 中为 hue 命令使用多个分类变量?
Is there a way to use more than one categorical variable for the hue command in seaborn?
我正在尝试创建我感兴趣的变量的条带图,在 x 轴上按组分隔,并根据性别着色。
Treatment
sex
variable
Drug
male
0.24
Drug
male
-0.42
Drug
male
0.43
Drug
male
1.39
Drug
male
1.62
Drug
female
1.53
Drug
female
0.08
Drug
female
0.68
Drug
female
2.46
Drug
female
0.02
Placebo
male
1.74
Placebo
male
-0.90
Placebo
male
-2.39
Placebo
male
-0.58
Placebo
male
-0.95
Placebo
female
0.71
Placebo
female
1.47
Placebo
female
-0.62
Placebo
female
0.73
Placebo
female
0.44
我使用以下代码创建情节:
g = sns.stripplot(data = df, x = "treatment", y ="variable", hue = "sex", order = ["Drug","Placebo"],hue_order=["male", "female"], palette=dict(male="#466a6e", female="#76b4ba"), dodge=True)
但是,我想对药物组使用绿色阴影,对安慰剂组使用灰色阴影(即:药物男性 = #466a6e,药物女性 = #76b4ba,安慰剂男性 = #706e6d,安慰剂女性= #bfbcba) - 有办法吗?
我最接近的是创建一个连接“治疗”和“性别”的新列,从而创建 4 个级别以用于 hue 命令。然而,这给我留下了下面的图,其中带状图没有在 x 轴刻度上对齐:
df['Treat+Sex'] = df['treatment'] + df['sex']
g = sns.stripplot(data = df, x = "treatment", y = "variable", hue = "Treat+Sex", order=["Drug","Placebo"], hue_order=["Drugmale","Drugfemale","Placebomale", "Placebofemale"], palette=dict(Drugmale="#466a6e", Drugfemale="#76b4ba", Placebomale="#706e6d", Placebofemale="#bfbcba"), dodge=True)
非常感谢任何帮助。
由于 "Treat+Sex"
色调列中现在有 4 个值,Seaborn 将为 4 个不同的闪避位置制作 space。 Drug
的2会放在刻度位置的左边,Placebo
的2会放在右边。
这是另一种方法。您可以使用数据框的子集创建 sns.stripplot
两次。一次用于 Drug
处理,一次用于 Placebo
处理,每次使用不同的颜色。 order=
参数将确保所有内容都绘制在正确的位置。
请注意 sns.stripplot
returns 和 ax
,因为它是 axes-level function. The tuple legend handler 可用于帮助创建组合图例。
from matplotlib import pyplot as plt
from matplotlib.legend_handler import HandlerTuple
import seaborn as sns
import pandas as pd
df = pd.read_html('
ax = sns.stripplot(data=df[df["Treatment"] == "Drug"],
x="Treatment", y="variable", hue="sex", order=["Drug", "Placebo"],
hue_order=["male", "female"], palette=dict(male="#466a6e", female="#76b4ba"), dodge=True)
sns.stripplot(data=df[df["Treatment"] == "Placebo"],
x="Treatment", y="variable", hue="sex", order=["Drug", "Placebo"],
hue_order=["male", "female"], palette=dict(male="#706e6d", female="#bfbcba"), dodge=True, ax=ax)
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=[tuple(handles[i::2]) for i in range(2)], labels=labels[:2], title='Gender',
handlelength=3, handler_map={tuple: HandlerTuple(ndivide=None, pad=0.2)})
plt.show()
我正在尝试创建我感兴趣的变量的条带图,在 x 轴上按组分隔,并根据性别着色。
Treatment | sex | variable |
---|---|---|
Drug | male | 0.24 |
Drug | male | -0.42 |
Drug | male | 0.43 |
Drug | male | 1.39 |
Drug | male | 1.62 |
Drug | female | 1.53 |
Drug | female | 0.08 |
Drug | female | 0.68 |
Drug | female | 2.46 |
Drug | female | 0.02 |
Placebo | male | 1.74 |
Placebo | male | -0.90 |
Placebo | male | -2.39 |
Placebo | male | -0.58 |
Placebo | male | -0.95 |
Placebo | female | 0.71 |
Placebo | female | 1.47 |
Placebo | female | -0.62 |
Placebo | female | 0.73 |
Placebo | female | 0.44 |
我使用以下代码创建情节:
g = sns.stripplot(data = df, x = "treatment", y ="variable", hue = "sex", order = ["Drug","Placebo"],hue_order=["male", "female"], palette=dict(male="#466a6e", female="#76b4ba"), dodge=True)
但是,我想对药物组使用绿色阴影,对安慰剂组使用灰色阴影(即:药物男性 = #466a6e,药物女性 = #76b4ba,安慰剂男性 = #706e6d,安慰剂女性= #bfbcba) - 有办法吗?
我最接近的是创建一个连接“治疗”和“性别”的新列,从而创建 4 个级别以用于 hue 命令。然而,这给我留下了下面的图,其中带状图没有在 x 轴刻度上对齐:
df['Treat+Sex'] = df['treatment'] + df['sex']
g = sns.stripplot(data = df, x = "treatment", y = "variable", hue = "Treat+Sex", order=["Drug","Placebo"], hue_order=["Drugmale","Drugfemale","Placebomale", "Placebofemale"], palette=dict(Drugmale="#466a6e", Drugfemale="#76b4ba", Placebomale="#706e6d", Placebofemale="#bfbcba"), dodge=True)
非常感谢任何帮助。
由于 "Treat+Sex"
色调列中现在有 4 个值,Seaborn 将为 4 个不同的闪避位置制作 space。 Drug
的2会放在刻度位置的左边,Placebo
的2会放在右边。
这是另一种方法。您可以使用数据框的子集创建 sns.stripplot
两次。一次用于 Drug
处理,一次用于 Placebo
处理,每次使用不同的颜色。 order=
参数将确保所有内容都绘制在正确的位置。
请注意 sns.stripplot
returns 和 ax
,因为它是 axes-level function. The tuple legend handler 可用于帮助创建组合图例。
from matplotlib import pyplot as plt
from matplotlib.legend_handler import HandlerTuple
import seaborn as sns
import pandas as pd
df = pd.read_html('
ax = sns.stripplot(data=df[df["Treatment"] == "Drug"],
x="Treatment", y="variable", hue="sex", order=["Drug", "Placebo"],
hue_order=["male", "female"], palette=dict(male="#466a6e", female="#76b4ba"), dodge=True)
sns.stripplot(data=df[df["Treatment"] == "Placebo"],
x="Treatment", y="variable", hue="sex", order=["Drug", "Placebo"],
hue_order=["male", "female"], palette=dict(male="#706e6d", female="#bfbcba"), dodge=True, ax=ax)
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=[tuple(handles[i::2]) for i in range(2)], labels=labels[:2], title='Gender',
handlelength=3, handler_map={tuple: HandlerTuple(ndivide=None, pad=0.2)})
plt.show()