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:
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:
Am I using errorbar wrong? Is there a better tool for this?
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.
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.
By default, the barplot() function draws error bars in the plot with 95% confidence interval.
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.
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:
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:
Earlier Answer with Exaggerated Errors
The months are being interpreted differently by seaborn
and matplotlib
resulting 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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With