Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Plot the Confidence Interval in Python?

#1
How to Plot the Confidence Interval in Python?

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()
https://blog.finxter.com/wp-content/uplo...00x219.png 300w" sizes="(max-width: 715px) 100vw, 715px" />

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:

https://blog.finxter.com/wp-content/uplo...00x225.png 300w" sizes="(max-width: 696px) 100vw, 696px" />

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:

https://blog.finxter.com/wp-content/uplo...00x198.png 300w" sizes="(max-width: 483px) 100vw, 483px" />

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:

The post How to Plot the Confidence Interval in Python? first appeared on Finxter.



https://www.sickgaming.net/blog/2021/02/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Open a PDF File in Python? xSicKxBot 0 12 02-22-2021, 11:39 AM
Last Post: xSicKxBot
  [Tut] Python Get Milliseconds xSicKxBot 0 25 02-19-2021, 08:28 AM
Last Post: xSicKxBot
  [Tut] Python vars() Function xSicKxBot 0 18 02-18-2021, 04:10 PM
Last Post: xSicKxBot
  [Tut] Python object() Function xSicKxBot 0 25 02-16-2021, 08:16 PM
Last Post: xSicKxBot
  [Tut] Python str() Function xSicKxBot 0 24 02-14-2021, 06:05 AM
Last Post: xSicKxBot
  [Tut] Python issubclass() xSicKxBot 0 27 02-12-2021, 10:40 AM
Last Post: xSicKxBot
  [Tut] Python int() Function xSicKxBot 0 32 02-04-2021, 08:21 PM
Last Post: xSicKxBot
  [Tut] How to Send UDP Multicast in Python? xSicKxBot 0 31 02-03-2021, 01:31 PM
Last Post: xSicKxBot
  [Tut] How To Cut A String In Python? xSicKxBot 0 28 02-01-2021, 04:09 PM
Last Post: xSicKxBot
  [Tut] Python filter() xSicKxBot 0 42 02-01-2021, 03:25 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
News - A Super Mario Lenticular Magnet S...
Last Post: xSicKxBot
Today 07:14 PM
» Replies: 0
» Views: 0
News - Halo Infinite's Open World Honors...
Last Post: xSicKxBot
Today 07:14 PM
» Replies: 0
» Views: 0
Blender 2.92 Released
Last Post: xSicKxBot
Today 01:09 PM
» Replies: 0
» Views: 4
Fedora - Contribute at the Fedora Audio,...
Last Post: xSicKxBot
Today 01:09 PM
» Replies: 0
» Views: 2
News - George Romero's Lost Movie The Am...
Last Post: xSicKxBot
Today 01:09 PM
» Replies: 0
» Views: 9
[Tut] How to Read a File Without Newline...
Last Post: xSicKxBot
Today 06:41 AM
» Replies: 0
» Views: 8
(Indie Deal) FREE Syberia 2 | Risk of Ra...
Last Post: xSicKxBot
Today 06:41 AM
» Replies: 0
» Views: 9
(Free Game Key) Black Desert Online - Fr...
Last Post: xSicKxBot
Today 06:41 AM
» Replies: 0
» Views: 9
News - Splatoon 2 Version 5.4.0 Is Now L...
Last Post: xSicKxBot
Today 03:09 AM
» Replies: 0
» Views: 8
News - Nightdive Studios Adds Online Mul...
Last Post: xSicKxBot
Today 03:09 AM
» Replies: 0
» Views: 10

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020