在单个图中,所有列的箱线图由“标签"分开.柱子 [英] In a single figure, boxplot of all columns split by a "label" column

查看:42
本文介绍了在单个图中,所有列的箱线图由“标签"分开.柱子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看

除了我希望它被包含特定标签的列分隔,类似于在以下示例中通过 hue 参数实现的功能:

  ax = sns.boxplot(x ="day",y ="total_bill",hue ="smoker",data = tips,调色板="Set3") 

此示例仅在定义 x y 时有效,但是对于我想要实现的目标,我希望 x 本质上是每一列在我的数据框中(显然不包括标签),以 y 作为频率,类似于第一个示例中所示.如果无法通过seaborn实现,我愿意尝试其他一些针对python的可视化库.

解决方案

您需要拆栈"或融化"数据,使所有内容都是值,而不是变量(长格式而不是宽格式).

这是下面的样子:

  iris_xtab = seaborn.load_dataset("iris")iris_long = pandas.melt(iris,id_vars ='species')seaborn.boxplot(x ='species',y ='value',hue ='variable',data = iris_long) 

或者将种类值忽略为x(您必须按照前面建议的那样分配一个虚拟值

  ax = seaborn.boxplot(x ='pos',y ='value',hue ='variable',数据= iris_long.assign(pos = 1)) 

Looking at the boxplot API page, I want something that looks like a combination of this:

>>> iris = sns.load_dataset("iris")
>>> ax = sns.boxplot(data=iris, orient="h", palette="Set2")

Except I want it split by a column that holds a certain label, similarly to what is achieved by the hue argument in an example that follows:

ax = sns.boxplot(x="day", y="total_bill", hue="smoker", data=tips, palette="Set3")

This example only works when x and y are defined, but for what I want to achieve I want x to essentially be every column in my dataframe (except the label, obviously) and y to be the frequency, similarly to what is shown in the first example. If not possible to achieve with seaborn, I am willing to try some other visualization library for python.

解决方案

You need to "unstack" or "melt" the data such everything is value, not a variable (long format instead of wide format).

Here's what that looks like:

iris_xtab = seaborn.load_dataset("iris")
iris_long = pandas.melt(iris, id_vars='species')
seaborn.boxplot(x='species', y='value', hue='variable', data=iris_long)

Or leaving out the species value as x (you have to assign a dummy value as suggested earlier

ax = seaborn.boxplot(x='pos', y='value', hue='variable', 
                     data=iris_long.assign(pos=1))

这篇关于在单个图中,所有列的箱线图由“标签"分开.柱子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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