To display summaries of our data visually, we often plot statistics like the mean. These statistics may be more or less precise. Frequently, we quantify this uncertainty through confidence intervals, which account for the spread and number of data points included in the computation of our summary statistic.
We can illustrate confidence intervals using features like error bars and shaded regions. For example, the bar.plot()
function in seaborn automatically plots an error line on each bar of the plot. The line (called an error bar) has a length that represents a 95% confidence interval for that statistic.
Additional parameters for the style of the error bars include color (errcolor
), width (errwidth
), and cap length (capsize
). For example, we could use the following code to make a bar plot of mean sales_totals
for each server
with thick red error bars with long caps.
sns.barplot(data=df, x='server', y='sales_totals', errcolor='red', errwidth=5, capsize=0.5)
We can also show confidence intervals on line plots using shaded regions. Seaborn’s line.plot()
function automatically plots shading for confidence intervals when the values are averages. The style can be changed from a shaded error band to vertical error bars using the err_style
parameter as shown in the following code.
sns.lineplot(data=df, x='month', y='sales_totals', err_style='bars')
Let’s create a couple of plots and adjust how the confidence intervals are displayed.
Instructions
After running the first two code cells, write the code to make a bar plot of the mean number of firespots
for each state
in the fires
dataset. Put the state names on the y-axis so that the bars are horizontal in the plot.
Let’s define the error bars differently. Create the same plot as you did in step 1, but adjust the error bar style as follows:
- change the color to blue
- change the width to 2
- add caps with length 0.3
From the same dataset, make a line plot of average number of firespots
by month
, where month
is on the x-axis.
Create the same line plot as you did in step 3, but change the confidence interval style from bands to bars.