首页 > 解决方案 > 由对照组均值归一化的 pandas seaborn pointplot

问题描述

我正在努力寻找一种解决方案,通过对照组的平均值来规范化 pandas 数据帧的分组 seaborn 点图

为了更清楚,这是我的 seaborn plot 在此处输入图像描述的结果,由于这段代码:

sns.pointplot(x='CELL TYPE',y='VELOCITY', hue = 'condition',ci=99 ,data=df_condition_cut1,ax = ax,order=['FRONT', 'MIDDLE', 'BACK'],
                               ordered=True)

我想要获得的是这种图,但通过每个条件下的 FRONT 单元类型的平均值进行了归一化。

请在此处找到带有简化图的采样重要数据集

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

df_cells = pd.DataFrame(data = {'VELOCITY':[1.4,1.6,1.3,1.1,0.8,0.7,2.3,1.8,1.7,1.5,1.6,1.7],
                         'condition':['A1','A1','A1','A1','A1','A1','A2','A2','A2','A2','A2','A2'],
                         'CELL TYPE':['FRONT','FRONT','MIDDLE','MIDDLE','BACK','BACK','FRONT','FRONT','MIDDLE','MIDDLE','BACK','BACK']})
print(df_cells)
fig, ax = plt.subplots(figsize=(25,12))
sns.pointplot(x='CELL TYPE',y='VELOCITY', hue = 'condition',ci=99 ,data=df_cells,ax = ax,order=['FRONT', 'MIDDLE', 'BACK'],
                                   ordered=True)

采样数据集

你对如何获得这个有什么建议吗?

谢谢伊曼纽尔

标签: pythonpandasplotseaborn

解决方案


感谢这篇文章

https://stackoverflow.com/a/55971778/15416347

我找到了解决方案。

在我的特定情况下,此代码有效:

t2=df_cells.loc[df_cells['CELL TYPE']=='FRONT',['VELOCITY','CELL TYPE','condition']].groupby('condition').VELOCITY.mean()
df_cells['VELOCITY_norm']=df_cells.VELOCITY/t2.reindex(df_cells.condition).values

df_cells.VELOCITY_norm
fig, ax = plt.subplots(figsize=(25,12))
sns.pointplot(x='CELL TYPE',y='VELOCITY_norm', hue = 'condition',ci=99 ,data=df_cells,ax = ax,order=['FRONT', 'MIDDLE', 'BACK'],
                                   ordered=True)

归一化


推荐阅读