How to Plot the Confidence Interval in Python?

Rate this post

Problem Formulation: How to plot the confidence interval in Python?

To plot a filled interval with the width ci and interval boundaries from y-ci to y+ci around function values y, use the plt.fill_between(x, (y-ci), (y+ci), color='blue', alpha=0.1) function call on the Matplotlib plt module.

  • The first argument x defines the x values of the filled curve. You can use the same values as for the original plot.
  • The second argument y-ci defines the lower interval boundary.
  • The third argument y+ci defines the upper interval boundary.
  • The fourth argument color='blue' defines the color of the shaded interval.
  • The fifth argument alpha=0.1 defines the transparency to allow for layered intervals.

from matplotlib import pyplot as plt
import numpy as np

# Create the data set
x = np.arange(0, 10, 0.05)
y = np.sin(x)

 Define the confidence interval
ci = 0.1 * np.std(y) / np.mean(y)

# Plot the sinus function
plt.plot(x, y)

# Plot the confidence interval
plt.fill_between(x, (y-ci), (y+ci), color='blue', alpha=0.1)

plt.show()

You can also plot two layered confidence intervals by calling the plt.fill_between() function twice with different interval boundaries:

from matplotlib import pyplot as plt
import numpy as np

# Create the data set
x = np.arange(0, 10, 0.05)
y = np.sin(x)

# Define the confidence interval
ci = 0.1 * np.std(y) / np.mean(y)

# Plot the sinus function
plt.plot(x, y)

# Plot the confidence interval
plt.fill_between(x, (y-ci), (y+ci), color='blue', alpha=0.1)
plt.fill_between(x, (y-2*ci), (y+2*ci), color='yellow', alpha=.1)

plt.show()

The resulting plot shows two confidence intervals in blue and yellow:

You can run this in our interactive Jupyter Notebook:

You can also use Seaborn’s regplot() function that does it for you, given a scattered data set of (x,y) tuples.

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

#create some random data
x = np.random.randint(1, 10, 20)
y = x + np.random.normal(0, 1, 20)

#create regplot
ax = sns.regplot(x, y)

This results in the convenient output:

Note that the 95% confidence interval is calculated automatically. An alternative third ci argument in the sns.regplot(x, y, ci=80) allows you to define another confidence interval (e.g., 80%).


To boost your skills in Python, Matplotlib and data science, join our free email academy and download your Python cheat sheets now!

Resources: