分层分组箱线图,

时间:2021-06-11 13:57:38

标签: python matplotlib seaborn data-visualization graphing

我有如下数据:

    engages_telehealth  knockout_tox    pcc  variable    value
0                 True          True   True    health   135.50
1                 True          True   True  admitted  3443.25
2                 True          True  False    health   136.50
3                 True          True  False  admitted  3444.45
4                 True         False   True    health   115.50
5                 True         False   True  admitted  3640.80
6                 True         False  False    health   117.75
7                 True         False  False  admitted  3615.60
8                False          True   True    health   137.00
9                False          True   True  admitted  3314.90
10               False          True  False    health   136.00
11               False          True  False  admitted  3320.40
12               False         False   True    health   115.00
13               False         False   True  admitted  3334.25
14               False         False  False    health   115.00
15               False         False  False  admitted  3363.25

我想制作一个类似于下图的分层聚类箱线图,这是描述生物学中多种条件的某种标准方式。 enter image description here

针对分层图(thisthis)的其他 SO 问题可能有三层,但这些是集群而不是独立条件,我的条件是布尔值而非数字。

我试过猫图:

  print(df_graph)

fig = plt.figure()
ax=fig.add_subplot(111)

sns.catplot(data=df_graph,x='variable',y='value',col=[ck1,ck2], kind='bar', ax=ax)
plt.show() 

但是,col 参数只接受一个字符串,我不想创建一个 FacetGrid,因为我在准备的文档中没有足够的空间。

1 个答案:

答案 0 :(得分:1)

这应该会让你很接近。您不会看到任何错误栏,因为每个类别只有一条记录。如果你这样做,他们就会出现。

import seaborn as sns
sns.set(rc={'figure.figsize':(16,9)})
sns.set_context('talk')


df = df.replace({True:'+', False:'-'})
df['cat'] = 'TELE' + df['engages_telehealth']+'\nTOX '+df['knockout_tox']+'\nPCC '+df['pcc']


sns.barplot(data=df, x='cat', y='value', hue='variable')

输出enter image description here