如何从三个数据帧构建Seaborn中的嵌套方框图

2024-10-01 13:46:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三个数据集,现在我正在制作三个不同的方框图。现在我正在使用:

chart1 = sns.catplot(x="Provider", y="Appearances", kind="box", data=mlt_sample1k)
chart2 = sns.catplot(x="Provider", y="Appearances", kind="box", data=mlt_sample10k)
chart3 = sns.catplot(x="Provider", y="Appearances", kind="box", data=mlt_sample100k)

其中mlt_sample1kmlt_sample10kmlt_sample100k是我的三个数据帧

我想将它们组合成一个单独的方框图,每个提供程序有3个平行方框,如示例from the docs.中所示,类似于:

enter image description here

但是有3个盒子,Thu、Fri等将是我的“提供者”类别。我在他们使用的文档中看到:

ax = sns.boxplot(x="day", y="total_bill", hue="smoker",

                 data=tips, palette="Set3")

但是,这对我不起作用,因为我必须指定三个数据集,每个框一个。我该怎么做

编辑:“我的数据帧”的结构始终相同:

item  |  provider  | appearances

'dog'    'prov1'      0.001
'cat'    'prov2'      0.02
'pig'    'prov1'      0.03
...

根据外观列,方框图表示每个数据帧中每个提供程序(总共6个)的项目统计信息

这三个数据帧的长度不同


Tags: 数据程序boxdataprovidersnskindcatplot
1条回答
网友
1楼 · 发布于 2024-10-01 13:46:08

IIUC,您可以concat三个数据帧和assign一列用作色调,如下所示:

sns.catplot(x="Provider", y="Appearances", 
            kind="box", hue='h',
            data=pd.concat([mlt_sample1k.assign(h='1k'), 
                            mlt_sample10k.assign(h='10k'), 
                            mlt_sample100k.assign(h='100k')])
           )

相关问题 更多 >