与seaborn分组的箱线图 [英] Grouped boxplot with seaborn

查看:165
本文介绍了与seaborn分组的箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python panda DataFrame中有以下数据.我想在 https://stanford中对箱形图进行分组. edu/〜mwaskom/software/seaborn/examples/grouped_boxplot.html

I have following data in python panda DataFrame. I would like to have grouped box plot similar to one in https://stanford.edu/~mwaskom/software/seaborn/examples/grouped_boxplot.html

对于每个id,我想并排绘制两个箱形图.我该如何做到这一点.我尝试用seaborn软件包进行绘图,但没有成功.

For each id, I would like to have two box plots plotted side by side. How do i achieve this. I tried plotting it with the seaborn package but without any success.

id               predicted                              real
1            [10, 10, 10]                      [16, 18, 20]   
2            [12, 12, 15]              [15, 17, 19, 21, 23]
3           [20, 5, 4, 4]                          [29, 32]
4    [25, 25, 25, 24, 21]  [21, 24, 25, 26, 28, 29, 30, 33]
5        [20, 20, 20, 21]  [21, 22, 24, 26, 28, 30, 31, 32]
6           [8, 3, 3, 14]                          [25, 27]
7  [1, 4, 4, 4, 5, 6, 10]                      [69, 71, 72]
8        [11, 11, 11, 11]              [19, 21, 22, 23, 24]
9            [7, 6, 9, 9]                  [19, 26, 27, 28]
10    [30, 30, 30, 30, 30]                          [38, 39]

推荐答案

请注意您正在查看的示例中的表结构

Notice the table structure in the example you are looking at

import seaborn as sns

tips = sns.load_dataset("tips")
sns.boxplot(x="day", y="total_bill", hue="sex", data=tips, palette="PRGn")
sns.despine(offset=10, trim=True)

tips.head()

我们的目标是像这样设置您的餐桌

Our goal is to set your table up like this

from StringIO import StringIO
import pandas as pd

text = """id               predicted                              real
1            [10, 10, 10]                      [16, 18, 20]   
2            [12, 12, 15]              [15, 17, 19, 21, 23]
3           [20, 5, 4, 4]                          [29, 32]
4    [25, 25, 25, 24, 21]  [21, 24, 25, 26, 28, 29, 30, 33]
5        [20, 20, 20, 21]  [21, 22, 24, 26, 28, 30, 31, 32]
6           [8, 3, 3, 14]                          [25, 27]
7  [1, 4, 4, 4, 5, 6, 10]                      [69, 71, 72]
8        [11, 11, 11, 11]              [19, 21, 22, 23, 24]
9            [7, 6, 9, 9]                  [19, 26, 27, 28]
10    [30, 30, 30, 30, 30]                          [38, 39]"""

df = pd.read_csv(StringIO(text), sep='\s{2,}', engine='python', index_col=0)

df = df.stack().str.strip('[]') \
       .str.split(', ').unstack()
df

df1 = df.stack().apply(pd.Series).stack().astype(int) \
          .rename_axis(['id', 'reality', None]) \
          .rename('value').reset_index(['id', 'reality']) \
          .reset_index(drop=True)

df1.head()

sns.boxplot(x='id', y='value', hue='reality', data=df1, palette='PRGn')
sns.despine(offset=10, trim=True)

这篇关于与seaborn分组的箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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