是否可以用每组中的观察值来注释一个海底小提琴图? [英] Is it possible to annotate a seaborn violin plot with number of observations in each group?

查看:114
本文介绍了是否可以用每组中的观察值来注释一个海底小提琴图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用每组的观察数来注释我的小提琴图.因此,问题本质上与这个问题,除了:

I would like to annotate my violin plot with the number of observations in each group. So the question is essentially the same as this one, except:

  • python代替R,
  • seaborn而不是ggplot,和
  • 用小提琴代替盒图

让我们以 Seaborn API文档中的示例为例:

Lets take this example from Seaborn API documentation:

import seaborn as sns
sns.set_style("whitegrid")
tips = sns.load_dataset("tips")
ax = sns.violinplot(x="day", y="total_bill", data=tips)

我想在小提琴的上面有n = 62,n = 19,n = 87和n = 76.这可行吗?

I'd like to have n=62, n=19, n=87, and n=76 on top of the violins. Is this doable?

推荐答案

在这种情况下,我想预先计算带注释的值并将其合并到分类轴中.换句话说,预先计算例如星期四,N = xxx"

In this situation, I like to precompute the annotated values and incorporate them into the categorical axis. In other words, precompute e.g., "Thurs, N = xxx"

看起来像这样:

import seaborn as sns
sns.set_style("whitegrid")
ax= (
    sns.load_dataset("tips")
       .assign(count=lambda df: df['day'].map(df.groupby(by=['day'])['total_bill'].count()))
       .assign(grouper=lambda df: df['day'].astype(str) + '\nN = ' + df['count'].astype(str))
       .sort_values(by='day') 
       .pipe((sns.violinplot, 'data'), x="grouper", y="total_bill")
       .set(xlabel='Day of the Week', ylabel='Total Bill (USD)')   
)

这篇关于是否可以用每组中的观察值来注释一个海底小提琴图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆