Scatter plots are used to show the values of discrete data, and line plots are used to show the values of continuous data where ‘in-between’ values can be interpolated.
Similarly, point estimates show uncertainty at one point while confidence bands show uncertainty over a range of points. For line plots, confidence bands are preferable. We’ve already seen Seaborn doing this automatically for us.
To draw confidence bands in Matplotlib, we make use of the Axes.fill_between method. This takes at least two arguments but to draw error bands we’ll use it with three.
The first argument is a sequence of x-positions of a curve.
The second and third are corresponding y-positions of the curve. The area between these two y-positions will be filled in.
As an aside, if we call the ‘fill_between‘ method with only two arguments, the area between the curve and the x-axis will be filled.
Demonstration: Errors on line plots
Let us bring back another dataset we used previously to demonstrate this with our time series data of temperatures in New York for the first week of June 2016.
Step 1
First, let us import the date class from the ‘datetime’ module.
Code:
fromdatetimeimportdate
Step 2
We then draw the figure and axes for the plot we are intending to display.
We don’t have any actual data for what sort of error there might be in the data, so we’ll generate some by assuming there might be a ±5% error for any data point.
Here you wrote a program using the fill_between method on a line plot. Can you think of examples or use cases where you might want to use this method in a combined plot of both scatter and line?