简体   繁体   English

条形图上的seaborn重叠群图

[英]seaborn overlap swarmplot on barplot

I would like to overlap a swarmplot on a barplot with seaborn.我想在带有seaborn的条形图上重叠一个群图。 Let's generate my kind of data (3 conditions, with 3 replicates each, at 3 timepoints) :让我们生成我的数据类型(3 个条件,每个条件 3 个重复,在 3 个时间点):

condition_list = []
experiment_list = []
day_list = []
result_list = []


for condition in ['A', 'B', 'C']:
        
    for experiment in range(1,4):

        for day in range(1,4):

            condition_list.append(condition)
            experiment_list.append(experiment)
            day_list.append(day)
            result_list.append(random.random())
            
            
datas = list(zip(condition_list,experiment_list,day_list,result_list))

df = pd.DataFrame(data=datas, columns=['Condition','Experiment','Day','Result'])

I am fine with the barplot :我对 barplot 很好:

sns.catplot(x="Condition", y="Result", hue='Day', data=df, kind="bar", ci='sd')

which looks like that :看起来像这样: 在此处输入图片说明

But if I want to add corresponding values as dots on this figure, they are all aligned on the same abscissa :但是如果我想在这个图上添加相应的值作为点,它们都在同一个横坐标上对齐:

sns.catplot(x="Condition", y="Result", hue='Day', data=df, kind="bar", ci='sd')
sns.swarmplot(x="Condition", y="Result", hue='Day', data=df)

在此处输入图片说明

Here is my problem !这是我的问题! For each bar, I would like to plot as dots the three corresponding values.对于每个条形,我想将三个相应的值绘制为点。

For example, if we restrain datas for the first day, I can plot bars AND dots on the same figure :例如,如果我们限制第一天的数据,我可以在同一图上绘制条形和点:

new_df = df[df['Day'] == 1].reset_index(drop=True)

sns.barplot(x="Condition", y="Result", data=new_df, ci="sd")
sns.swarmplot(x="Condition", y="Result",  color='black',data=new_df, zorder=10)

在此处输入图片说明

Is it possible to do the same on the first figure with seaborn ?是否有可能对 seaborn 的第一个数字做同样的事情? Thank you very much for your help ^^非常感谢您的帮助^^

Thanks to JohanC's comment, dodge function of swarm plot splits dots according to their category.感谢 JohanC 的评论,swarm plot 的 dodge 函数根据它们的类别分割点。

ax = sns.catplot(x="Condition", y="Result", hue='Day', data=df, kind="bar", ci='sd')
ax = sns.swarmplot(x="Condition", y="Result", hue='Day', color='black', dodge=True,data=df)
ax.get_legend().set_visible(False)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM