简体   繁体   English

如何在 seaborn barplot 上绘制误差线?

[英]How to plot errorbars on seaborn barplot?

I have the following dataframe:我有以下数据框:

data = {'Value':[6.25, 4.55, 4.74, 1.36, 2.56, 1.4, 3.55, 3.21, 3.2, 3.65, 3.45, 3.86, 13.9, 10.3, 15], 
        'Name':['Peter', 'Anna', 'Luke',
                 'Peter', 'Anna', 'Luke',
                 'Peter', 'Anna', 'Luke',
                 'Peter', 'Anna', 'Luke',
                 'Peter', 'Anna', 'Luke'],
        'Param': ['Param1', 'Param1', 'Param1', 
                 'Param2', 'Param2', 'Param2', 
                 'Param3', 'Param3', 'Param3', 
                 'Param4', 'Param4', 'Param4',
                 'Param5', 'Param5', 'Param5'],
        'error': [2.55, 1.24, 0, 0.04, 0.97, 0, 0.87, 0.7, 0, 0.73, 0.62, 0, 0, 0, 0]}
df = pd.DataFrame(data)

I'd like to add errorbars (pre-defined in the error column) to the bar plot, but I can't seem to get the x-coordinates right?我想将误差条(在误差列中预定义)添加到条形图中,但我似乎无法正确获取 x 坐标? It shows errorbars for Param5 but there are no errors for Param5 ?它显示 Param5 的错误Param5 ,但Param5没有错误? Also for Luke , there are no errors, but in Param1 an errorbar is plotted.同样对于Luke ,没有错误,但在Param1中绘制了一个错误栏。

plt.figure()
ax = sns.barplot(x = 'Param', y = 'Value', data = df, hue = 'Name', palette = sns.color_palette('CMRmap_r', n_colors = 3))
x_coords = [p.get_x() + 0.5*p.get_width() for p in ax.patches]
y_coords = [p.get_height() for p in ax.patches]
plt.errorbar(x=x_coords, y=y_coords, yerr=df["error"], fmt="none", c= "k")

在此处输入图像描述

The bars in ax.patches come ordered by hue value. ax.patches中的条按hue值排序。 To get the bars and the dataframe in the same order, the dataframe could be sorted first by Name and then by Param :要以相同的顺序获取条形图和数据框,数据框可以先按Name排序,然后按Param排序:

from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd

data = {'Value': [6.25, 4.55, 4.74, 1.36, 2.56, 1.4, 3.55, 3.21, 3.2, 3.65, 3.45, 3.86, 13.9, 10.3, 15],
        'Name': ['Peter', 'Anna', 'Luke',
                 'Peter', 'Anna', 'Luke',
                 'Peter', 'Anna', 'Luke',
                 'Peter', 'Anna', 'Luke',
                 'Peter', 'Anna', 'Luke'],
        'Param': ['Param1', 'Param1', 'Param1',
                  'Param2', 'Param2', 'Param2',
                  'Param3', 'Param3', 'Param3',
                  'Param4', 'Param4', 'Param4',
                  'Param5', 'Param5', 'Param5'],
        'error': [2.55, 1.24, 0, 0.04, 0.97, 0, 0.87, 0.7, 0, 0.73, 0.62, 0, 0, 0, 0]}
df = pd.DataFrame(data)
df = df.sort_values(['Name', 'Param'])

plt.figure()
ax = sns.barplot(x='Param', y='Value', data=df, hue='Name', palette='CMRmap_r')
x_coords = [p.get_x() + 0.5 * p.get_width() for p in ax.patches]
y_coords = [p.get_height() for p in ax.patches]
ax.errorbar(x=x_coords, y=y_coords, yerr=df["error"], fmt="none", c="k")
plt.show()

带有自定义误差线的 sns.barplot

PS: Note that by default, the columns are sorted alphabetically. PS:请注意,默认情况下,列按字母顺序排序。 If you want to maintain the original order, you can make the column categorical via pd.Categorical(df['Name'], df['Name'].unique()) .如果要保持原始顺序,可以通过pd.Categorical(df['Name'], df['Name'].unique())使列分类。

df = pd.DataFrame(data)
df['Name'] = pd.Categorical(df['Name'], df['Name'].unique())
df['Param'] = pd.Categorical(df['Param'], df['Param'].unique())
df = df.sort_values(['Name', 'Param'])

sns.barplot 以原始顺序带有错误栏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM