2016-06-16 21 views
4

我希望创建一个seabornpointplot以显示列中的完整数据分布,以及最低25%值的分布和最高25%值的分布,并排(x轴)。 到目前为止,我的尝试为我提供了这些值,但它们仅显示在x轴的同一部分上,并未在图上从左向右展开,并且没有明显的方式来标记x-ticks中的点(我更喜欢,而不是通过传奇)。Seaborn Plot包括相同数据的不同分布

import seaborn as sns 
import pandas as pd 
import matplotlib.pyplot as plt 
%matplotlib notebook 

df = sns.load_dataset('tips') 
df1 = df[(df.total_bill < df.total_bill.quantile(.25))] 
df2 = df[(df.total_bill > df.total_bill.quantile(.75))] 

sns.pointplot(y=df['total_bill'], data=df, color='red') 
sns.pointplot(y=df1['total_bill'], data=df1, color='green') 
sns.pointplot(y=df2['total_bill'], data=df2, color='blue') 

enter image description here

回答

3

你可以.join()新分配到你使用现有的df然后.plot()宽幅:

lower, upper = df.total_bill.quantile([.25, .75]).values.tolist() 
df = df.join(df.loc[df.total_bill < lower, 'total_bill'], rsuffix='_lower') 
df = df.join(df.loc[df.total_bill > upper, 'total_bill'], rsuffix='_upper') 
sns.pointplot(data=df.loc[:, [c for c in df.columns if c.startswith('total')]]) 

获得:

enter image description here

我F你想添加组,你可以简单地使用.unstack()long格式:

df = df.loc[:, ['total_bill', 'total_bill_upper', 'total_bill_lower']].unstack().reset_index().drop('level_1', axis=1).dropna() 
df.columns = ['grp', 'val'] 

获得:

sns.pointplot(x='grp', y='val', hue='grp', data=df) 

enter image description here

2

我会沿着添加的行思“组“,然后绘制为单个DataFrame。

import seaborn as sns 
import pandas as pd 
import matplotlib.pyplot as plt 
%matplotlib notebook 

df = sns.load_dataset('tips') 
df = df.append(df) 

df.loc[(df.total_bill < df.total_bill.quantile(.25)),'group'] = 'L' 
df.loc[(df.total_bill > df.total_bill.quantile(.75)),'group'] = 'H' 
df = df.reset_index(drop=True) 
df.loc[len(df)/2:,'group'] = 'all' 

sns.pointplot(data = df, 
       y='total_bill', 
       x='group', 
       hue='group', 
       linestyles='') 

figure output

相关问题