 • ## Summary

Search form
• 00:00

[OPENING MUSIC]

• 00:05

GARY WHITE: So in the previous tutorial,we looked at the error bars, and settingthe different parameters, so decidingthe color of the error, and also the pointsin the actual figure.In this tutorial, we're going to look at the continuous errors.So I've imported matplotlib and numpy,and I'm also using a Gaussian process regressor from sklearn,

• 00:27

GARY WHITE [continued]: so make sure you have the sklearn package installed.And then we just define the model and draw some data.So we define the model using this lambda function,and it takes times sin(x).Then our x data is just this array here.Then our y data is a model of that x data.

• 00:47

GARY WHITE [continued]: So then we create our Gaussian process fit,we fit the model to our data, and thenwe create our xfit of linearly-spaced valuesbetween 0 and 10, and then we're makingpredictions for this xfit, and returningthe standard deviation.So we then create the dyfit, which

• 01:10

GARY WHITE [continued]: is the 95% confidence interval.So we're going 2 times the square rootof the standard deviation, to give us the 95% confidenceregion.And so to actually draw this, we can first of all do plt.plot().And, in here, we just want to plot the x data and y data.

• 01:35

GARY WHITE [continued]: And we can also specify some options.So in this case, we're going to do circles,and we want them to be red.So we specify 'or'.We can then plot our fit.So it's plt.plot().So this is the fit of the function that has been created.

• 01:56

GARY WHITE [continued]: So we have xfit and yfit.In this case, we're just going to havea line, so you specify a dash.And you want the color to be gray.So we just say color='gray'.

• 02:20

GARY WHITE [continued]: And, finally, we want to plot our confidence intervals.So, to do that, we're going to use the plt.fill_between.So this will create a bounding box, or underline,that shows where the confidence interval is.

• 02:41

GARY WHITE [continued]: And so our points are the xfit and yfit.xfit and then the yfit minus the dyfit, which is the error.So dyfit, and then the yfit plus the dyfit.

• 03:11

GARY WHITE [continued]: And, finally, we can specify some formatting options.So, in this case, we're going to say that the color isequal to gray, as well.And we want to change the alpha valueto make it a bit translucent, so that youcan see the actual value behind it, sort of in the same waythat you can see the gray behind this value.

• 03:33

GARY WHITE [continued]: So we set the alpha value equal to 0.2.We're then just going to add a limit to our x-axis.So xlim from 0 to 10-- as that's where we specified our linearspace from--so 0 to 10.And then we can just do plt.show().

• 03:57

GARY WHITE [continued]: So if we run that, you see that we got this continuous errorbar, and the fill_between has filled between these areas.So the actual data is these red circles.So this is the actual data.

• 04:18

GARY WHITE [continued]: This is the model that was plotted to us,using the Gaussian process regressor,is the actual line here.So we can see it captures each of the points quite well.And then the 95% confidence region is the shaded area here.So you can see in this point where the line is quite

• 04:38

GARY WHITE [continued]: straight and smooth, there's quite a low confi--or, the confidence region--95% confidence region doesn't take up too much space.But here, where there's quite a large change,you can see that the confidence region increases quite a bitbetween the two points.So you can see that the confidenceregion at the actual points reduces to almost 0,because it knows that these points are part of the model.

• 05:00

GARY WHITE [continued]: And so that's how you create a continuous confidenceregion for a plot.And you should definitely do thisif you're working in any scientific documents,or something that you want to get published, because it'soften something that reviewers will point out,is that they want to have confidenceintervals in the actual plots, to give them a better idea,rather than just using the average values.

### Video Info

Episode: 39

Publisher: Gary White

Publication Year: 2020

Video Type:Tutorial

### Segment Info

Segment Num.: 1

Persons Discussed:

Events Discussed:

Keywords:

## Abstract

Gary White explains how to set continuous error bars in Matplotlib.