How to plot confidence bands with Matplotlib ?

Published: March 07, 2023

Tags: Python; Matplotlib;

DMCA.com Protection Status

Confidence bands describe a technique used in statistical inference to indicate the reliability of an estimate. They are also known as confidence intervals, and provide an upper and lower bound on the range of plausible values for a given statistic. Confidence bands can be used to quantify uncertainty associated with a sample size, or with one or more parameter estimates within a population

Plotting confidence bands

For anyone looking to plot confidence bands with matplotlib, a solution is using fill_between. This tool makes plotting and customizing your graphs easily. It allows you to create customized confidence bands tailored to your specific data and requirements:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 60, 100)
y = np.sin(x/20*np.pi)

error = np.random.normal(0.1, 0.1, size=y.shape)
y_meas = y + np.random.normal(0, 0.1, size=y.shape)

plt.plot(x, y, 'r--')
plt.fill_between(x, y-0.4, y+0.4,color='#D3D3D3')
plt.scatter(x,y_meas, c='k',s=10)

plt.xlim(0,60)

#plt.savefig('RegressionConfidenceBands.png')
plt.show()

output:

How to plot confidence bands with Matplotlib ?
How to plot confidence bands with Matplotlib ?

References

Links Site
Confidence and prediction bands wikipedia
fill_between() matplotlib.org
Image

of