seaborn箱线图的子图 [英] Subplot for seaborn boxplot

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

问题描述

我有一个这样的数据框

import seaborn as sns
import pandas as pd
%pylab inline

df = pd.DataFrame({'a' :['one','one','two','two','one','two','one','one','one','two'], 
                   'b': [1,2,1,2,1,2,1,2,1,1], 
                   'c': [1,2,3,4,6,1,2,3,4,6]})

单个箱线图是可以的:

sns.boxplot(y="b", x="a", data=df, orient='v')

但我想为所有变量构建一个子图.我试过了:

But I want to build a subplot for all variables. I tried:

names = ['b', 'c']
plt.subplots(1,2)
sub = []

for name in names:
    ax = sns.boxplot(  y=name, x= "a", data=df,  orient='v' )
    sub.append(ax)

但它输出:

推荐答案

我们用子图创建图形:

f, axes = plt.subplots(1, 2)

其中轴是包含每个子图的数组.

Where axes is an array with each subplot.

然后我们用参数 ax 告诉每个图我们想要它们在哪个子图中.

Then we tell each plot in which subplot we want them with the argument ax.

sns.boxplot(  y="b", x= "a", data=df,  orient='v' , ax=axes[0])
sns.boxplot(  y="c", x= "a", data=df,  orient='v' , ax=axes[1])

结果是:

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

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