Python 同一图(seaborn)上 Pandas 数据框多列的箱线图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49554139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boxplot of Multiple Columns of a Pandas Dataframe on the Same Figure (seaborn)
提问by Duccio Piovani
I feel I am probably not thinking of something obvious. I want to put in the same figure, the box plot of every column of a dataframe, where on the x-axis I have the columns' names. In the seaborn.boxplot()
this would be equal to groupby
by every column.
我觉得我可能没有想到一些显而易见的事情。我想放入同一个图,即数据框每一列的箱线图,在 x 轴上我有列的名称。在seaborn.boxplot()
这将等于groupby
每一列。
In pandas I would do
在熊猫我会做
df = pd.DataFrame(data = np.random.random(size=(4,4)), columns = ['A','B','C','D'])
df.boxplot()
which yields
这产生
Now I would like to get the same thing in seaborn. But when I try sns.boxplot(df)
, I get only one grouped boxplot. How do I reproduce the same figure in seaborn?
现在我想在seaborn中得到同样的东西。但是当我尝试时sns.boxplot(df)
,我只得到一个分组的箱线图。如何在seaborn中重现相同的图形?
回答by ImportanceOfBeingErnest
The seaborn equivalent of
相当于seaborn
df.boxplot()
is
是
sns.boxplot(x="variable", y="value", data=pd.melt(df))
Complete example:
完整示例:
import numpy as np; np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame(data = np.random.random(size=(4,4)), columns = ['A','B','C','D'])
sns.boxplot(x="variable", y="value", data=pd.melt(df))
plt.show()
This works because pd.melt
converts a wide-form dataframe
这是有效的,因为pd.melt
转换了宽格式的数据帧
A B C D
0 0.374540 0.950714 0.731994 0.598658
1 0.156019 0.155995 0.058084 0.866176
2 0.601115 0.708073 0.020584 0.969910
3 0.832443 0.212339 0.181825 0.183405
to long-form
长格式
variable value
0 A 0.374540
1 A 0.156019
2 A 0.601115
3 A 0.832443
4 B 0.950714
5 B 0.155995
6 B 0.708073
7 B 0.212339
8 C 0.731994
9 C 0.058084
10 C 0.020584
11 C 0.181825
12 D 0.598658
13 D 0.866176
14 D 0.969910
15 D 0.183405
回答by RafaP
You could use the built-in pandas method df.plot(kind='box')as suggested in this question.
I realize this answer will not help you if you haveto use seaborn, but it may be useful for people with simpler requirements.
您可以按照此问题中的建议使用内置的熊猫方法df.plot(kind='box')。
我意识到如果您必须使用 seaborn,这个答案对您没有帮助,但它可能对要求更简单的人有用。
import numpy as np; np.random.seed(42)
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(data = np.random.random(size=(4,4)), columns = ['A','B','C','D'])
df.plot(kind='box')
plt.show()