Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to input values for confidence interval/ error bars on seaborn barplot?

I'm used to doing my barplots on seaborn and I like it's layout for showing confidence bars, but I have a special case in a dataset where I already have the confidence interval, like this:

month   ci-b     mean    ci-t
201801  0.020   0.0206  0.021
201802  0.019   0.0198  0.0204
201803  0.022   0.0225  0.0228
201804  0.022   0.0236  0.0240
201805  0.023   0.0235  0.0239

Is there a way to manually input the values for seaborn confidence interval lines? Or to use it as "None" and use some matlib function to put the confidence interval in the graph (but keeping seaborn's barplot)

When I do:

ax = sns.barplot('month','mean',data=df, ci=None)

I get, as expected, a normal barplot:

This graphic

And when I attempt to use matlib's error bar like this:

ax = sns.barplot('month','mean',data=df, ci=None)
plt.errorbar(x=df['month'],y=df['mean'],yerr=(df['ci-t']-df['ci-b']))

Everything get's messed up with just one strange line lost in the figure:

Like this graphic

Am I using errorbar wrong? Is there a better tool for this?

like image 305
Carolina Avatar asked Oct 11 '18 19:10

Carolina


People also ask

Can you use confidence intervals as error bars?

Confidence limits provide a range of values estimated from a study group that is highly likely to include the true, but unknown, value (“confidence limit” applies to the results of a statistical analysis). They are usually displayed as error bars on a graph.

How does Seaborn calculate confidence interval?

That was about Matplotlib; Seaborn uses bootstrapping to calculate the 95% confidence interval of data. In essence, it's a method of repeatedly resampling from a sample of the population, which gives good estimates of the true mean and 95% confidence.

What function can add the error bars for the 95% confidence intervals on a barplot?

By default, the barplot() function draws error bars in the plot with 95% confidence interval.

How do you add values to a bar chart in Seaborn?

In seaborn barplot with bar, values can be plotted using sns. barplot() function and the sub-method containers returned by sns. barplot(). Import pandas, numpy, and seaborn packages.


1 Answers

Update (August 2, 2019):

My earlier answer (see below) overstates the error because yerr uses the same error for the top and bottom if passed a single array of shape (N,). To get different errors for the bottom and top, we need to use an array of shape (2, N). The first row is used for the bottom error and the second for the top (from the documentation. In code this is:

# Bottom error, then top
yerr = [df['mean']-df['ci-b'], df['ci-t'] - df['mean']]

ax = sns.barplot('month','mean',data=df, ci=None)
plt.errorbar(x=[0, 1, 2, 3, 4],y=df['mean'],
            yerr=yerr, fmt='none', c= 'r')

The result is below: enter image description here

The errors are now different on the bottom and top.

Following is a direct comparison, with the original (symmetrical) error bars in red and the non-symmetrical error bars in blue. We can directly see the differences:

enter image description here

Earlier Answer with Exaggerated Errors

The months are being interpreted differently by seaborn and matplotlibresulting in odd placement of the error bars. You also need to specify fmt='none' to avoid having errorbar plot data points as a line. The following code places the errors bars at the correct x locations:

ax = sns.barplot('month','mean',data=df, ci=None)
plt.errorbar(x=[0, 1, 2, 3, 4],y=df['mean'],
             yerr=(df['ci-t']-df['ci-b']), fmt='none', c= 'r')

enter image description here

like image 76
willk Avatar answered Sep 30 '22 04:09

willk