Python 将预先计算的误差条与Seaborn和Barplot一起使用
我有一个数据框,其中我预先计算了一组特定值的平均值和标准偏差。数据框的一个片段以及如何创建它已在下面进行了说明:Python 将预先计算的误差条与Seaborn和Barplot一起使用,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我有一个数据框,其中我预先计算了一组特定值的平均值和标准偏差。数据框的一个片段以及如何创建它已在下面进行了说明: import pandas as pd import seaborn as sns import matplotlib.pyplot as plt channel = ["Red", "Green", "Blue", "Red", "Green", "Blue",
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
channel = ["Red", "Green", "Blue", "Red", "Green", "Blue", "Red", "Green", "Blue"]
average= [83.438681, 36.512924, 17.826646, 83.763724, 36.689707, 17.892932, 84.747069, 37.072383, 18.070416]
sd = [7.451285, 3.673155, 1.933273, 7.915111, 3.802536, 2.060639, 7.415741, 3.659094, 2.020355]
conc = ["0.00", "0.00", "0.00", "0.25", "0.25", "0.25", "0.50", "0.50", "0.50"]
df = pd.DataFrame({"channel": channel,
"average": average,
"sd" : sd,
"conc": conc})
order = ["0.00", "0.25", "0.50"]
sns.barplot(x="conc", y="average", hue="channel", data=df, ci=None, order=order);
运行上述代码会生成如下图像:
我有一个列sd
,其中有预先计算的标准偏差,我想在绘制的每个条的上方和下方添加误差条。但是我不知道怎么做
任何帮助都将不胜感激。昨天遇到此错误。在seaborn中,我相信您不能根据预先确定的错误添加错误条。最简单的解决方案是将matplotlib条形图绘制在seaborn条形图上
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
channel = ["Red", "Green", "Blue", "Red", "Green", "Blue", "Red", "Green", "Blue"]
average= [83.438681, 36.512924, 17.826646, 83.763724, 36.689707, 17.892932, 84.747069, 37.072383, 18.070416]
sd = [7.451285, 3.673155, 1.933273, 7.915111, 3.802536, 2.060639, 7.415741, 3.659094, 2.020355]
conc = ["0.00", "0.00", "0.00", "0.25", "0.25", "0.25", "0.50", "0.50", "0.50"]
df = pd.DataFrame({"channel": channel,
"average": average,
"sd" : sd,
"conc": conc})
order = ["0.00", "0.25", "0.50"]
sns.barplot(x="conc", y="average", hue="channel", data=df, ci=None,
order=order)
conc2=[0,0,0,1,1,1,2,2,2]
width = .25
add = [-1*width, 0 , width, -1*width, 0 , width, -1*width, 0 , width,]
x = np.array(conc2)+np.array(add)
plt.errorbar(x = x, y = df['average'],
yerr=df['sd'], fmt='none', c= 'black', capsize = 2)
plt.show()
有点傻,但很管用 嘿,这只会在中间的“绿色”条上创建错误条。有没有办法得到所有的酒吧?编辑代码!如果你喜欢使用seaborn,那么你可以遵循我写的。否则,我认为您最好只使用matplotlib.pyplot。不那么容易,但支持errorbars。