6.6 C
London
Tuesday, March 11, 2025
HomePythonConfidence Intervals in PythonHow to Plot a Confidence Interval in Python

How to Plot a Confidence Interval in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

confidence interval is a range of values that is likely to contain a population parameter with a certain level of confidence.

This tutorial explains how to plot a confidence interval for a dataset in Python using the seaborn visualization library.

Plotting Confidence Intervals Using lineplot()

The first way to plot a confidence interval is by using the lineplot() function, which connects all of the data points in a dataset with a line and displays a confidence band around each point:

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

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

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

Confidence interval plot in Python

By default, the lineplot() function uses a 95% confidence interval but can specify the confidence level to use with the ci command.

The smaller the confidence level, the more narrow the confidence interval will be around the line. For example, here’s what an 80% confidence interval looks like for the exact same dataset:

#create lineplot
ax = sns.lineplot(x, y, ci=80)

Confidence interval in Python using seaborn

Plotting Confidence Intervals Using regplot()

You can also plot confidence intervals by using the regplot() function, which displays a scatterplot of a dataset with confidence bands around the estimated regression line:

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

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

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

Scatterplot with confidence interval in Python

Similar to lineplot(), the regplot() function uses a 95% confidence interval by default but can specify the confidence level to use with the ci command.

Again, the smaller the confidence level the more narrow the confidence interval will be around the regression line. For example, here’s what an 80% confidence interval looks like for the exact same dataset:

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

Plot confidence interval in Python

Additional Resources

What are Confidence Intervals?
How to Calculate Confidence Intervals in Python

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories