首页 > 解决方案 > 在 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。任何帮助深表感谢。谢谢。

标签: pandasseaborn

解决方案


如果您交换数据透视表中的行和列,您将获得所需的格式。这符合您的问题的意图吗?

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)

在此处输入图像描述


推荐阅读