Matplotlib fill_between for Confidence Intervals

When I first started working with Python for data visualization, one of the most powerful tools I discovered was Matplotlib. Over the years, I’ve used it extensively to create clear, informative plots that communicate data insights effectively. One feature I rely on heavily is the fill_between function, especially when plotting confidence intervals around a line or curve.

Confidence intervals are crucial for understanding the uncertainty in data or model predictions. Visualizing them helps stakeholders see not just the trend but also the reliability of that trend. In this article, I’ll walk you through how to use Matplotlib’s fill_between to plot confidence intervals in Python, using practical examples relevant to real-world data scenarios.

What is Matplotlib fill_between and Why Use It?

Matplotlib is the go-to Python library for plotting static, animated, and interactive visualizations. The fill_between function fills the area between two horizontal curves, which is perfect for shading confidence intervals.

Imagine you are analyzing monthly average temperatures across the USA and want to show the average temperature line along with the range where temperatures typically vary (the confidence interval). Using fill_between, you can shade that range, making your plot not only visually appealing but also informative.

Plot Confidence Intervals with Matplotlib fill_between in Python

Let me share how I approach this task. I’ll cover two common methods:

  • Using manually calculated confidence intervals
  • Using regression results to plot confidence bands

Both methods are simple and practical.

Method 1: Plot Confidence Intervals with Manually Calculated Bounds

This method is useful when you have data with mean values and their confidence bounds already computed or estimated.

import numpy as np
import matplotlib.pyplot as plt

# Example data: Monthly average temperatures in a US city (°F)
months = np.arange(1, 13)
avg_temps = np.array([30, 35, 45, 55, 65, 75, 80, 78, 70, 58, 45, 35])

# Simulated confidence intervals (lower and upper bounds)
lower_bound = avg_temps - 5
upper_bound = avg_temps + 5

plt.figure(figsize=(10, 6))
plt.plot(months, avg_temps, label='Average Temperature', color='blue')
plt.fill_between(months, lower_bound, upper_bound, color='blue', alpha=0.2, label='95% Confidence Interval')

plt.title('Monthly Average Temperature with Confidence Interval')
plt.xlabel('Month')
plt.ylabel('Temperature (°F)')
plt.xticks(months)
plt.legend()
plt.grid(True)
plt.show()

You can see the output in the screenshot below.

Matplotlib fill_between for Confidence Intervals

In this example, I created a simple dataset representing monthly temperatures and added fixed confidence intervals of ±5°F. The fill_between function shades the area between the lower and upper bounds, making the uncertainty visually clear.

Method 2: Plot Confidence Intervals from Regression Analysis

Often, you want to visualize confidence intervals derived from a model. For example, let’s say we fit a linear regression to some US housing price data over time and want to plot the predicted prices with confidence intervals.

Here’s how I do it with Python’s statsmodels library alongside Matplotlib:

import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm

# Sample data: Year vs Median Housing Price (in $1000s)
years = np.arange(2010, 2021)
prices = np.array([180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280])

# Add constant for intercept
X = sm.add_constant(years)
model = sm.OLS(prices, X).fit()

# Predict prices and get confidence intervals
predictions = model.get_prediction(X)
pred_summary = predictions.summary_frame(alpha=0.05)  # 95% CI

plt.figure(figsize=(10, 6))

# Plot observed data
plt.scatter(years, prices, label='Observed Prices', color='black')

# Plot predicted line
plt.plot(years, pred_summary['mean'], label='Predicted Price', color='green')

# Fill confidence interval
plt.fill_between(years, pred_summary['mean_ci_lower'], pred_summary['mean_ci_upper'],
                 color='green', alpha=0.3, label='95% Confidence Interval')

plt.title('US Median Housing Prices with Confidence Interval')
plt.xlabel('Year')
plt.ylabel('Price ($1000s)')
plt.legend()
plt.grid(True)
plt.show()

You can see the output in the screenshot below.

fill_between for Confidence Intervals in Matplotlib

In this example, I fit a simple linear regression model to housing price data from 2010 to 2020. Using statsmodels, I obtain the predicted means and their confidence intervals, then plot them with fill_between. This method gives a statistically sound confidence band around the regression line.

Tips for Using fill_between Effectively in Python

  • Adjust Transparency: Use the alpha parameter to control the opacity of the shaded area. I usually set it between 0.2 and 0.4 to keep the plot clean.
  • Color Coordination: Match the fill color closely with the line color to maintain visual cohesion.
  • Label the Confidence Interval: Always add a label for the filled area so it appears in the legend.
  • Use Clear Axes Labels: Make sure your axes and title clearly indicate what the confidence interval represents.
  • Handle Missing Data: Ensure your lower and upper bounds arrays are the same length as your x-axis data to avoid errors.

Why Visualizing Confidence Intervals Matters in Python Data Science

From my experience, showing confidence intervals builds trust in your data analysis. Whether you’re presenting monthly sales forecasts, temperature trends, or economic indicators, your audience benefits from seeing the range of uncertainty.

Matplotlib’s fill_between makes it easy to add this dimension to your plots without complicating your code. It’s a must-have technique for any Python developer serious about data visualization.

Other Python data visualization tutorials you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.