python - 将 errorbar() 查询添加到 stripplot

标签 python matplotlib seaborn

我一直在努力为我的分类变量 (x=Type) 获取所需的错误栏,其中有两个不同的标记形状(每个类别 A 和 B),并且错误栏颜色与相应标记颜色的颜色相匹配 (即palette=dict(A= "k", B= "r"))

这是我的数据:

Type         Y              Err
A             3           0.2
A             3.1         0.8
A             4.3         0.6
B             5.9         1.1
B             5.1         0.5

我彻底搜索了这个论坛以获取错误栏,这就是我到目前为止所得到的。

Subplot1 = sns.stripplot(y=DataFrame["Y"], x=DataFrame["Type"], marker='s',
                         palette=dict(A= "k", B="r")`

# Add errorbars to each data point 
for PointPair in Subplot1.collections:
     for x, y in PointPair.get_offsets():
         x_coords.append(x)
         y_coords.append(y)

Subplot1.errorbar(x_coords, y_coords, yerr=DataFrame['Err'], 
                     fmt=' ', 
                     elinewidth=1, ecolor='k', 
                     capsize=3, markeredgewidth=0.81, 
                      )

所以用简单的英语来说,我一直在尝试调整 ecolor='k' 以及 marker='s' 以获得单独的错误栏颜色和标记形状类别 A 和 B。但以下似乎不起作用: ecolor=['k', 'r'] marker=['s', 'o']

如果可以的话,请解释一下这一点。 非常感谢!

最佳答案

不幸的是,如果您试图摆脱所提供的绘图,seaborn 很难使用。

就您而言,我认为完全放弃 seaborn 并使用标准 matplotlib 函数生成绘图会更容易:

markers = ['s','o']
colors = ['k', 'r']
grouped = df.groupby('Type')

fig, ax = plt.subplots()
for i,((g,d),m,c) in enumerate(zip(grouped,markers,colors)):
    # generate scattered x values, adjust scale= as needed
    x = np.random.normal(loc=i,scale=0.05,size=(len(d['Y'],))) 
    ax.errorbar(x,d['Y'],yerr=d['Err'],
                fmt=m, color=c, capsize=3)
ax.set_xticks(list(range(len(grouped))))
ax.set_xticklabels([a for a in grouped.groups])

enter image description here

关于python - 将 errorbar() 查询添加到 stripplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57246281/

相关文章:

python - Matplotlib 绘图 : AttributeError: 'list' object has no attribute 'xaxis'

python - 限制用户多次点赞某个帖子

python - 具有归一化行的 2D 直方图

python - PyCharm + Matplotlib?

python - Matplotlib 条形图 X 轴标签顺序

seaborn - 如何在 Python 中使用 Seaborn 在条形图中显示带有标准差的误差条

python - scikit-learn 中的 hmmlearn 中缺少数据

python - 如何将微秒转换为hh:mm:ss以导航到youtube视频中的特定时间?

python - 绘制圆弧 : anomaly in the plot

python - 带有热图调色板的条形图