python - 由对照组均值归一化的 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)
你对如何获得这个有什么建议吗?
谢谢伊曼纽尔
解决方案
感谢这篇文章
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)
推荐阅读
- xml - strings.xml:这有什么不正确的?
- python-3.x - 使用线程时如何解决winerror32?
- sql - SQL - 从课程和主题中过滤
- javascript - 为什么 array.forEach() 在这段代码中什么都不做?
- github - 新团队成员的 GitHub 安全问题
- reactjs - 被 CORS 策略阻止:“Access-Control-Allow-Origin”
- c - C - 使用 while 和 switch/case 的菜单程序返回无效选择的重复菜单
- c# - 更新/插入/删除基于 c# 列表的 sql 记录和修改过的数据
- java - 将空对象转换为整数
- spring-boot - 如何在控制器中 Spring Boot + Data JPA 生成模式(例如表、字段、列)