Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to visualize 95% confidence interval in Matplotlib?
To visualize a 95% confidence interval in Matplotlib, you need to plot your main data line and fill the area around it representing the uncertainty range. This technique is commonly used in statistical plots to show the reliability of predictions or measurements.
Basic 95% Confidence Interval Plot
Here's how to create a confidence interval visualization with synthetic data ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
x = np.arange(0, 10, 0.05)
y = np.sin(x)
# Define the confidence interval (95% means ±1.96 standard deviations)
ci = 0.1 * np.std(y) / np.mean(y)
# Plot the main line
plt.plot(x, y, color='black', lw=2, label='Data')
# Fill the confidence interval area
plt.fill_between(x, (y-ci), (y+ci), color='blue', alpha=0.3, label='95% CI')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('95% Confidence Interval Visualization')
plt.legend()
plt.show()
Realistic Statistical Example
Here's a more practical example using regression data with proper confidence intervals ?
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
# Generate sample data with noise
np.random.seed(42)
x = np.linspace(0, 10, 50)
true_y = 2 * x + 1
noise = np.random.normal(0, 2, len(x))
y_observed = true_y + noise
# Perform linear regression
slope, intercept, r_value, p_value, std_err = stats.linregress(x, y_observed)
y_predicted = slope * x + intercept
# Calculate 95% confidence interval
# For 95% CI, use t-distribution critical value
n = len(x)
t_val = stats.t.ppf(0.975, n-2) # 95% confidence level
residuals = y_observed - y_predicted
mse = np.sum(residuals**2) / (n - 2)
se_pred = np.sqrt(mse * (1/n + (x - np.mean(x))**2 / np.sum((x - np.mean(x))**2)))
ci_95 = t_val * se_pred
# Create the plot
plt.figure(figsize=(10, 6))
plt.scatter(x, y_observed, alpha=0.6, color='red', label='Observed Data')
plt.plot(x, y_predicted, color='blue', linewidth=2, label='Regression Line')
plt.fill_between(x, y_predicted - ci_95, y_predicted + ci_95,
color='blue', alpha=0.2, label='95% Confidence Interval')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Linear Regression with 95% Confidence Interval')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Key Parameters
-
fill_between()− Fills area between two curves -
alpha− Controls transparency (0.2-0.3 works well for CI bands) -
color− Should complement your main line color -
label− Important for legend clarity
Common Use Cases
- Regression analysis − Show prediction uncertainty
- Time series forecasting − Display forecast confidence
- Scientific measurements − Represent measurement error
- A/B testing results − Show statistical significance
Conclusion
Use fill_between() with appropriate alpha transparency to visualize confidence intervals. For statistical accuracy, calculate proper confidence bounds using t-distribution critical values rather than simple standard deviation multiples.
Advertisements
