我想在Seaborn FacetGrid上的pandas数据框中的列中绘制误差条
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar']*2,
'B' : ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three'],
'C' : np.random.randn(8),
'D' : np.random.randn(8)})
df
Run Code Online (Sandbox Code Playgroud)
示例数据帧
A B C D
0 foo one 0.445827 -0.311863
1 bar one 0.862154 -0.229065
2 foo two 0.290981 -0.835301
3 bar three 0.995732 0.356807
4 foo two 0.029311 0.631812
5 bar two 0.023164 -0.468248
6 foo one -1.568248 2.508461
7 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Python 中的 Seaborn 包来可视化一些数据。特别是,我想使用该catplot(kind='bar')函数(以前命名为factorplot())。我的数据框看起来像这样(列'x','col','row'并且'hue'是绝对的):
x y dy col row hue
0 4 9 0.766591 1 0 2
1 5 9 0.688683 0 1 0
2 0 7 0.707982 0 0 1
3 3 6 0.767210 2 1 0
4 3 8 0.287153 0 1 0
Run Code Online (Sandbox Code Playgroud)
我想使用不确定性列'dy'来表示'y'. Seaborn catplots 执行的默认引导或标准偏差误差条不能为我提供令人满意的解决方案。
在这里,我提供了最小完全可验证的示例:
import pandas as pd
import numpy.random as npr
import seaborn as sns
npr.seed(seed=0) …Run Code Online (Sandbox Code Playgroud) 我有一个 Pandas 数据框,它有几个组列,如下所示。
gr1 grp2 variables lb m ub
A A1 V1 1.00 1.50 2.5
A A2 V2 1.50 2.50 3.5
B A1 V1 3.50 14.50 30.5
B A2 V2 0.25 0.75 1.0
Run Code Online (Sandbox Code Playgroud)
我正在尝试为variables使用中的每个变量获得一个单独的子条形图FacetGrid。我正在尝试构建我需要的最终图,如下所示。
这是我到目前为止。
g = sns.FacetGrid(df, col="variables", hue="grp1")
g.map(sns.barplot, 'grp2', 'm', order=times)
Run Code Online (Sandbox Code Playgroud)
但不幸的是,这堆积了我所有的数据点。
我该Seaborn怎么做呢?
更新:以下代码在很大程度上完成了我所追求的但目前不显示yerr.
g = sns.factorplot(x="Grp2", y="m", hue="Grp1", col="variables", data=df, kind="bar", size=4, aspect=.7, sharey=False)
Run Code Online (Sandbox Code Playgroud)
如何将lb和ub作为误差线合并到因子图上?