pandas - 在 python seaborn 中创建一个带有显示标准偏差的误差线的条形图
问题描述
我是 python 新手。我正在分析一个数据集,需要一些帮助来绘制带有显示 SD 的误差线的条形图。在以下链接中检查以下示例数据集https://drive.google.com/file/d/10JDr7d_vhEocWzChg-sfBEumsWVghFS8/view?usp=sharing
这是我正在使用的代码;
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
df = pd.read_excel('Sample_data.xlsx')
#Adding a column 'Total' by adding all cell counts in each row
#This will give the cells counted in each sample
df['Total'] = df['Cell1'] + df['Cell2'] + df['Cell3'] + df['Cell4']
df
# Creating a pivot table based on Timepoint and cell types
phenotype = df.pivot_table (index = ['Timepoint'],
values=['Cell1',
'Cell2',
'Cell3',
'Cell4'],
aggfunc = np.sum,
margins = False)
phenotype
# plot different cell types grouped according to the timepoint and error bars = SD
sns.barplot(data = phenotype)
现在我被困在根据时间点列绘制单元类型并将误差线 = SD。任何帮助深表感谢。谢谢。
解决方案
如果您交换数据透视表中的行和列,您将获得所需的格式。这符合您的问题的意图吗?
phenotype = df.pivot_table (index = ['Time point'],
values=['Cell1', 'Cell2', 'Cell3', 'Cell4'],
aggfunc = np.sum,
margins = False)
phenotype.reset_index()
phenotype = phenotype.stack().unstack(level=0)
phenotype
Time point 48 72 96
Cell1 54 395 57
Cell2 33 35 39
Cell3 1 3 9
Cell4 2 6 3
sns.boxplot(data = phenotype)
推荐阅读
- c# - 是否可以通过 Microsoft Bot Framework 自定义 Teams 中接收到的图像和 GIF 的大小
- r - 循环具有相同前缀的变量
- java - 使用 showInstallPrompt 从即时调用可安装应用程序的 Java 语法
- angular - 为什么我的 const 变量输出,不为空
- reactjs - AWS AppSync + React-Apollo Query/useQuery 引发异常 this.currentObservable.query.getCurrentResult 不是函数
- plot - 如何从 Prophet.plot_components() 中提取 xy 数据
- string - 如何从字符串创建 XSL 节点集
- ios - 核心数据:如何测试 NSManagedObjectContext 合并策略
- jenkins - 如何在 consul 中添加服务 - 使用 groovy jenkins 管道
- java - 如何将日历转换为带时区的 XML 日期格式?