问题
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