python - 在 Seaborn 和 Barplot 中使用预先计算的误差线
问题描述
我有一个数据框,我已经预先计算了一组特定值的平均值和标准偏差。数据框的片段以及如何创建它如下所示:
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 中,我相信您不能根据预先确定的错误添加错误栏。最简单的解决方案是在 seaborn 上绘制 matplotlib 条形图。
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()
有点愚蠢但有效!
推荐阅读
- api - 如何基于 http req 方法运行 next.js 中间件功能?就像为“POST”请求而不是“GET”调用中间件函数
- operating-system - thr minix3 proc.c 中的 set_idle_name 有什么作用?
- windows - C# Wuapi 提供了不正确的功能更新信息
- extjs - Sencha ExtJS 为什么选项返回null?
- jquery - 从特定日期在 Jquery 中设置最大日期
- grpc - 如何允许 Java 中的 gRPC Channel 使用多个连接?
- react-native - ReactNative:AsyncStorage 问题:我无法正确检索数据
- mysql - 用密钥 On ExpressJs for mysql 续集多个依赖关联问题
- javascript - 控制台日志数据返回为空
- android - 我的节目中有一个听众,我猜这个听众被错误地使用了