In a single figure, boxplot of all columns split by a “label” column

六眼飞鱼酱① 提交于 2021-02-08 07:35:27

问题


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.


回答1:


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))



来源:https://stackoverflow.com/questions/40241082/in-a-single-figure-boxplot-of-all-columns-split-by-a-label-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!