许多 pandas 数据框的箱形图 [英] Box Plot of a many Pandas Dataframes

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

问题描述

我有三个数据帧,其中包含17组A,B和C组数据。A在以下代码段中显示

 将熊猫作为pd 
导入numpy作为np
data1 = pd.DataFrame(np.random.rand(17,3),column = ['A','B','C'] )
data2 = pd.DataFrame(np.random.rand(17,3)+0.2,column = ['A','B','C'])
data3 = pd.DataFrame( np.random.rand(17,3)+0.4,column = ['A','B','C'])

我想绘制一个箱形图以比较这三个组,如下图

我正在尝试制作使用


I have three dataframes containing 17 sets of data with groups A, B, and C. A shown in the following code snippet

import pandas as pd
import numpy as np
data1 = pd.DataFrame(np.random.rand(17,3), columns=['A','B','C'])
data2 = pd.DataFrame(np.random.rand(17,3)+0.2, columns=['A','B','C'])
data3 = pd.DataFrame(np.random.rand(17,3)+0.4, columns=['A','B','C'])

I would like to plot a box plot to compare the three groups as shown in the figure below I am trying make the plot using seaborn's box plot as follows

import seaborn as sns
sns.boxplot(data1, groupby='A','B','C') 

but obviously this does not work. Can someone please help?

解决方案

Consider assigning an indicator like Location to distinguish your three sets of data. Then concatenate all three and melt the data to retrieve one value column, one Letter categorical column, and one Location column, all inputs into sns.boxplot:

import pandas as pd
import numpy as np
from matplotlib pyplot as plt
import seaborn as sns

data1 = pd.DataFrame(np.random.rand(17,3), columns=['A','B','C']).assign(Location=1)
data2 = pd.DataFrame(np.random.rand(17,3)+0.2, columns=['A','B','C']).assign(Location=2)
data3 = pd.DataFrame(np.random.rand(17,3)+0.4, columns=['A','B','C']).assign(Location=3)

cdf = pd.concat([data1, data2, data3])    
mdf = pd.melt(cdf, id_vars=['Location'], var_name=['Letter'])
print(mdf.head())

#    Location Letter     value
# 0         1      A  0.223565
# 1         1      A  0.515797
# 2         1      A  0.377588
# 3         1      A  0.687614
# 4         1      A  0.094116

ax = sns.boxplot(x="Location", y="value", hue="Letter", data=mdf)    
plt.show()

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

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