Creating Interactive Plotly Graphs with 95% Confidence Intervals
Written on
Chapter 1: Introduction to Plotly Graphs
Generating a Plotly graph with a 95% confidence interval (CI) in Python is quite simple. This guide will walk you through the process step-by-step.
Section 1.1: Prerequisites
To begin, ensure you have the necessary libraries installed. We will utilize the pandas and Plotly graph object libraries. Plotly is a powerful open-source tool for crafting interactive, high-quality graphs.
# Install pandas
pip install pandas
# or via conda
conda install -c anaconda pandas
# Install Plotly
pip install plotly
# or via conda
conda install -c plotly plotly
Next, import the required libraries:
import pandas as pd
import plotly.graph_objects as go
Section 1.2: Data Generation
In this example, we will create our own dataset for plotting. You can substitute this with your actual data. We will create three lists:
- X-axis data points: Years from 2001 to 2024.
- Y-axis data points: 25 data points representing the squares of numbers from 1 to 24.
- Standard Error (SE): For this example, we will assume the SE is one-fourth of the respective data point. Note that this is illustrative; proper statistical methods should be used to estimate SE.
def square(lst):
return [i ** 2 for i in lst]
def se(lst):
return [i / 4 for i in lst]
year = list(range(2001, 2025))
lst1 = square(list(range(1, 25)))
lst_se = se(lst1)
Section 1.3: Data Formatting for Plotting
The following function formats our lists (dates, point estimates, and standard errors). These lists are organized into a pandas DataFrame, and two extra columns for upper and lower confidence limits are computed. The function then outputs three lists:
def format_data(date, estimates, estimates_se):
cols = {'Date': date, 'Estimates': estimates, 'SE': estimates_se}
# Calculate upper and lower confidence limits
df = pd.DataFrame(cols)
df['LCL'] = df['Estimates'] - 1.96 * df['SE']
df['UCL'] = df['Estimates'] + 1.96 * df['SE']
periods = list(df['Date'])
periods.sort()
period_sorted = periods.copy()
periods.sort(reverse=True)
# Append sorted dates for plotting the confidence band
for i in range(len(periods)):
period_sorted.append(periods[i])periods = period_sorted.copy()
LCL = df['LCL'].tolist()
UCL = df['UCL'].tolist()
UCL.reverse()
for i in UCL:
LCL.append(i)CI = LCL.copy()
return periods, estimates, CI
Section 1.4: Creating the Plotly Graph
The Plotly library offers numerous options for graph creation. The function below constructs a Plotly graph utilizing the plotly.graph_objects.Figure class and the add_trace method. Here, we add the point estimates and the 95% confidence interval.
def plot1(y1, x1, CI_estimates, ci_dates, title, y_axis_label, x_axis_label, show_legend=True):
# Initialize figure
fig = go.Figure()
# Add Trace with point estimates
fig.add_trace(
go.Scatter(x=x1,
y=y1,
mode='lines',
hoverinfo='y+x',
opacity=0.5,
line=dict(color='blue', width=3),
name='Name of Line',
visible=True,
legendgroup="legendgroup1")
)
# Add Trace for the 95% confidence band
fig.add_trace(
go.Scatter(x=ci_dates,
y=CI_estimates,
mode='lines',
showlegend=False,
hoverinfo='y',
line=dict(color=None, width=0.10),
name='95% Confidence interval',
fill="toself",
legendgroup="legendgroup1",
fillcolor='lightblue',
opacity=0.25,
visible=True)
)
# Update layout for better aesthetics
fig.update_layout(
title_text=title,
legend_orientation="v",
font_size=14,
font={'family':'Calibri'},
title_x=0.5,
showlegend=show_legend,
xaxis_title=x_axis_label,
yaxis_title=y_axis_label,
yaxis={'linecolor': 'black', 'fixedrange': True, 'tickmode': 'auto', 'ticks': "outside", "showgrid": False},
xaxis={'linecolor': 'black', 'fixedrange': True, 'tickmode': 'array', "showgrid": False},
hovermode='x unified',
paper_bgcolor='white',
plot_bgcolor='white',
margin={'t': 125},
legend=dict(y=.00, x=0.77, traceorder='reversed'),
xaxis_range=[min(x1), max(x1)]
)
fig.update_xaxes(title_font_family="Calibri")
return fig.show()
Chapter 2: Visualizing the Graph
Now, we will run the format_data function using the generated data from the previous step as inputs. We will then use the formatted lists in the plot1 function.
my_formatted_data = format_data(date=year, estimates=lst1, estimates_se=lst_se)
plot1(y1=my_formatted_data[1],
x1=my_formatted_data[0],
CI_estimates=my_formatted_data[2],
ci_dates=year,
title='My Title Subtitle',
y_axis_label='Y-Axis Label',
x_axis_label='X-Axis Label',
show_legend=False)
... and there you have it!
These functions can be easily modified to include multiple lines with their corresponding 95% confidence intervals. If you have any questions, feel free to leave a comment.
Concluding Remarks
Plotly empowers users to create visually appealing and informative graphs effortlessly. I hope this guide has clarified how to generate a Plotly graph with a 95% confidence interval using Python.
Thank you for reading! If you enjoyed this content, please follow and support my work by hitting the clap button.
For more insights, visit PlainEnglish.io. Join our free weekly newsletter and connect with us on Twitter, LinkedIn, YouTube, and Discord. If you're looking to expand your software startup, check out Circuit.
The first video provides a detailed explanation of computing and plotting estimated 95% confidence intervals in Python.
The second video demonstrates how to use Plotly Express to create professional graphs in just a few minutes!