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 around function values
y, use the
plt.fill_between(x, (y-ci), (y+ci), color='blue', alpha=0.1) function call on the Matplotlib
- The first argument
xvalues of the filled curve. You can use the same values as for the original plot.
- The second argument
y-cidefines the lower interval boundary.
- The third argument
y+cidefines the upper interval boundary.
- The fourth argument
color='blue'defines the color of the shaded interval.
- The fifth argument
alpha=0.1defines 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!
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.