手动将误差线添加到 seaborn 线标记图

Add error bars manually to seaborn line marker plots

我有 xmeanstd 列。我想用使用 seaborn 的标记绘制线:

import seaborn as sns
import matplotlib.pyplot as plt

x = [0,1,2]
mean = [2,1,3]
std = [0.1,0.4,0.2]

sns.lineplot(x=x,y=mean,marker='o')

如何添加 std 作为误差线?

来自 seaborn 文档,

markers : boolean, list, or dictionary, optional
    Object determining how to draw the markers for different levels of the
    ``style`` variable. Setting to ``True`` will use default markers, or
    you can pass a list of markers or a dictionary mapping levels of the
    ``style`` variable to markers. Setting to ``False`` will draw
    marker-less lines.  Markers are specified as in matplotlib.

markerslineplotstyle 选项配合使用。在你的情况下,使用 marker,它被传递给 plt.plot,而不是 markers:

sns.lineplot(x=x,y=mean,marker='o')

输出:

我找到了适合我的答案:

df = pd.DataFrame({'x':x,'y':mean,'std':std})

g = sns.FacetGrid(df, size=5)
g.map(plt.errorbar, "x", "y", "std", marker="o")