Select Page

2.7 Bar Plots


A typical bar plot will do one of two things:  it will either depict a categorical variable on one axis, with a count variable on the other, or it will depict a categorical variable on one axis, with a summary statistic (such as a mean or median) on the other.

In seaborn, the first type of bar plot is built with the countplot() function.  Note that we can generate this plot by just passing in a single variable to the x-axis, along with the data source.

To show both bars with the same color value, just add a color parameter to the plot function.  A list of named colors supported in matplotlib can be found here.

In seaborn, the second type of bar plot is built with the barplot() function.  Typically, such a plot will be built with a categorical variable on the x-axis, and a numeric variable on the y-axis (although such a graph can be oriented horizontally, by passing the category variable as the y, and the numeric variable as the x).  By default, this function will depict a mean value for the numeric variable, along with a bar depicting a confidence interval (the confidence intervals tend to become smaller as the number of observations increases, and/or as the variance becomes smaller).

Here is an example of such a plot:

Included by default, the confidence interval bars are intended to offer context to the viewer – if the intervals overlap, we should pause before concluding that the difference between the two categories is truly meaningful.

Users who do not want the default setting of the error bars (95% confidence interval) can adjust their size by stating a value other than ci=95. If a user wanted to include more data in the interval, the user could type in the command ci=99. Similarly, if the user wanted to tweak the  interval in the opposite direction by displaying a lower confidence interval of 80, the command would be ci = 80.

Some additional modifications for the ‘dynamite sticks’ include:

  • Reflecting a standard deviation instead of the confidence interval (ci = ‘sd’)
  • Adjusting the orientation of the plot (orient = ‘v’ or orient = ‘h’)

However, many audiences find the confidence intervals to be an unwelcome distraction.  To remove them, just add ci= ‘none’ to the sns.barplot() function, as shown below: