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

问题描述

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

import seaborn as sns
import matplotlib.pyplot as plt

x = [0,1,2]
mean = [2,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.

markersstylelineplot 选项一起使用。在您的情况下,使用 marker,它传递给 plt.plot,而不是 markers

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

输出:

enter image description here

,

我找到了一个适合我的答案:

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

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

plot