包含同一d的不同分布的海生图

2024-10-04 01:27:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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


Tags: importdfdatamatplotlibascolortotaldf1
2条回答

您可以使用宽格式.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

如果要添加组,只需使用.unstack()获得long格式:

^{pr2}$

获得:

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

enter image description here

我的思路是添加一个“组”,然后绘制成一个数据帧。在

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

相关问题 更多 >