How to plot a time series array, with confidence intervals displayed in Python? (Matplotlib)

To plot a time series with confidence intervals in Python, we can use Matplotlib's plot() for the main line and fill_between() for the confidence bands. This visualization helps show data uncertainty and trends over time.

Step-by-Step Approach

Here's how to create a time series plot with confidence intervals ?

  • Create or load your time series data
  • Calculate rolling mean and standard deviation
  • Define upper and lower confidence bounds
  • Plot the mean line using plot()
  • Add confidence intervals using fill_between()

Example

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [10, 6]
plt.rcParams["figure.autolayout"] = True

# Generate sample time series data
time_points = np.linspace(-np.pi, np.pi, 400)
time_series_array = np.sin(time_points) + np.random.normal(0, 0.2, 400)

# Rolling window size for smoothing
n_steps = 15

# Convert to DataFrame for easier manipulation
time_series_df = pd.DataFrame(time_series_array, columns=['value'])

# Calculate rolling mean and standard deviation
rolling_mean = time_series_df['value'].rolling(n_steps).mean()
rolling_std = time_series_df['value'].rolling(n_steps).std()

# Define confidence interval (2 standard deviations)
confidence_interval = 2 * rolling_std
upper_bound = rolling_mean + confidence_interval
lower_bound = rolling_mean - confidence_interval

# Create the plot
plt.figure(figsize=(10, 6))

# Plot the rolling mean
plt.plot(rolling_mean.index, rolling_mean, 'b-', linewidth=2, label='Rolling Mean')

# Fill confidence interval
plt.fill_between(rolling_mean.index, lower_bound, upper_bound, 
                 color='lightblue', alpha=0.4, label='95% Confidence Interval')

# Add labels and title
plt.xlabel('Time Points')
plt.ylabel('Value')
plt.title('Time Series with Confidence Intervals')
plt.legend()
plt.grid(True, alpha=0.3)

plt.show()

Key Components

Rolling Statistics

The rolling() method calculates moving averages and standard deviations to smooth the data and estimate uncertainty ?

# Calculate rolling statistics
rolling_mean = data.rolling(window_size).mean()
rolling_std = data.rolling(window_size).std()

Confidence Intervals

Confidence intervals are typically calculated as mean ± (multiplier × standard deviation). Common multipliers are ?

Confidence Level Multiplier Coverage
68% 1 ±1 standard deviation
95% 2 ±2 standard deviations
99.7% 3 ±3 standard deviations

Alternative with Real Time Data

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Create time index
dates = pd.date_range('2023-01-01', periods=100, freq='D')
values = np.cumsum(np.random.normal(0, 1, 100)) + 100

# Create DataFrame
df = pd.DataFrame({'date': dates, 'value': values})

# Calculate 30-day rolling statistics
df['rolling_mean'] = df['value'].rolling(30).mean()
df['rolling_std'] = df['value'].rolling(30).std()

# 95% confidence interval
df['upper'] = df['rolling_mean'] + 1.96 * df['rolling_std']
df['lower'] = df['rolling_mean'] - 1.96 * df['rolling_std']

# Plot
plt.figure(figsize=(12, 6))
plt.plot(df['date'], df['value'], 'o-', alpha=0.6, label='Original Data')
plt.plot(df['date'], df['rolling_mean'], 'r-', linewidth=2, label='30-day Mean')
plt.fill_between(df['date'], df['lower'], df['upper'], 
                 alpha=0.3, color='red', label='95% Confidence')

plt.xlabel('Date')
plt.ylabel('Value')
plt.title('Time Series with 30-Day Rolling Confidence Intervals')
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

Conclusion

Use rolling() to calculate moving statistics and fill_between() to visualize confidence intervals. This approach effectively shows both trends and uncertainty in time series data, making it easier to identify significant patterns and outliers.

Updated on: 2026-03-25T23:12:10+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements