8
$\begingroup$

I'm using seaborn library to generate bar plots in python. I'm wondering what statistics are used to compute the error bars, but can't find any reference to this in the seaborn's barplot documentation.

I know the bar values are computed based on mean in my case (the default option), and I assume the error bars are computed based on a Normal distribution 95% confidence interval, but I'd like to be sure.

enter image description here

$\endgroup$
  • $\begingroup$ Just a coment. I just started learning seaborn and having the same question. Unfortunately I couldn't make much out of the only answer so far as for what test to use (perhaps it's my fault). Now for your question, I'd guess the test depends on what the estimator is and what're known beforehand. E.g. one could use 95% CI with a Z-test for normality to use the sample mean to estimate the population mean, but in this case the population std needs to be known in advance. However, if it's not known, then you've to use the t-test, using the distribution of $t:=\frac{\bar{x}-\mu}{s/\sqrt(n-1)}$. $\endgroup$ – Mathmath Feb 10 at 12:59
9
$\begingroup$

Looking at the source (seaborn/seaborn/categorical.py, line 2166), we find

def barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None,
        estimator=np.mean, ci=95, n_boot=1000, units=None,
        orient=None, color=None, palette=None, saturation=.75,
        errcolor=".26", ax=None, **kwargs):

so the default value is, indeed, .95, as you guessed.

EDIT: How CI is calculated: barplot calls utils.ci() which has

seaborn/seaborn/utils.py

def ci(a, which=95, axis=None):
    """Return a percentile range from an array of values."""
    p = 50 - which / 2, 50 + which / 2
    return percentiles(a, p, axis)

and this call to percentiles() is calling:

def percentiles(a, pcts, axis=None):
    """Like scoreatpercentile but can take and return array of percentiles.
    Parameters
    ----------
    a : array
        data
    pcts : sequence of percentile values
        percentile or percentiles to find score at
    axis : int or None
        if not None, computes scores over this axis
    Returns
    -------
    scores: array
        array of scores at requested percentiles
        first dimension is length of object passed to ``pcts``
    """
    scores = []
    try:
        n = len(pcts)
    except TypeError:
        pcts = [pcts]
        n = 0
    for i, p in enumerate(pcts):
        if axis is None:
            score = stats.scoreatpercentile(a.ravel(), p)
        else:
            score = np.apply_along_axis(stats.scoreatpercentile, axis, a, p)
        scores.append(score)
    scores = np.asarray(scores)
    if not n:
        scores = scores.squeeze()
    return scores

axis=None so score = stats.scoreatpercentile(a.ravel(), p) which is

scipy.stats.scoreatpercentile(a, per, limit=(), interpolation_method='fraction', axis=None)[source]
Calculate the score at a given percentile of the input sequence.

For example, the score at per=50 is the median. If the desired quantile lies between two data points, we interpolate between them, according to the value of interpolation. If the parameter limit is provided, it should be a tuple (lower, upper) of two values.

Parameters: 
a : array_like
A 1-D array of values from which to extract score.
per : array_like
Percentile(s) at which to extract score. Values should be in range [0,100].
limit : tuple, optional
Tuple of two scalars, the lower and upper limits within which to compute the percentile. Values of a outside this (closed) interval will be ignored.
interpolation_method : {‘fraction’, ‘lower’, ‘higher’}, optional
This optional parameter specifies the interpolation method to use, when the desired quantile lies between two data points i and j
fraction: i + (j - i) * fraction where fraction is the fractional part of the index surrounded by i and j.
lower: i.
higher: j.
axis : int, optional
Axis along which the percentiles are computed. Default is None. If None, compute over the whole array a.
Returns:    
score : float or ndarray
Score at percentile(s).

and looking in the source for scipy.stats.stats.py we see the signature

def scoreatpercentile(a, per, limit=(), interpolation_method='fraction',
                      axis=None):

so since seaboard calls it with no param for interpolation it is using fraction.

On a side note, there is a warning of future obsolescence in stats.scoreatpercentile(), namely

This function will become obsolete in the future. For Numpy 1.9 and higher, numpy.percentile provides all the functionality that scoreatpercentile provides. And it’s significantly faster. Therefore it’s recommended to use numpy.percentile for users that have numpy >= 1.9.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.