Complete reference for PlotlyVizPro utility functions.
Located in utils/plot_utils.py
Create an interactive line plot using Plotly Express.
Parameters:
df(DataFrame): Input datax(str): Column name for x-axisy(str): Column name for y-axiscolor(str, optional): Column for color groupingtitle(str, optional): Plot titlemarkers(bool): Show markers on line (default: True)template(str): Plotly template (default: "plotly_white")
Returns:
plotly.graph_objects.Figure: Interactive figure object
Example:
import pandas as pd
from utils.plot_utils import line_plot
df = pd.DataFrame({
'Date': pd.date_range('2024-01-01', periods=10),
'Sales': [100, 150, 120, 180, 200, 210, 190, 220, 240, 250]
})
fig = line_plot(df, x='Date', y='Sales', title='Sales Trend')
fig.show()Create an interactive scatter plot.
Parameters:
df(DataFrame): Input datax(str): Column name for x-axisy(str): Column name for y-axiscolor(str, optional): Column for color groupingsize(str, optional): Column for marker sizehover_name(str, optional): Column for hover labelstitle(str, optional): Plot titletemplate(str): Plotly template
Returns:
plotly.graph_objects.Figure: Interactive figure object
Example:
fig = scatter_plot(
df,
x='Sales',
y='Profit',
color='Category',
hover_name='Product',
title='Sales vs Profit'
)Create a bubble plot (scatter with size parameter).
Parameters:
df(DataFrame): Input datax(str): Column name for x-axisy(str): Column name for y-axissize(str): Column for bubble sizecolor(str, optional): Column for color groupingtitle(str, optional): Plot titletemplate(str): Plotly template
Returns:
plotly.graph_objects.Figure: Interactive figure object
Apply a dark theme to any Plotly figure.
Parameters:
fig(Figure): Plotly figure objectpaper_bgcolor(str): Background color for entire figureplot_bgcolor(str): Background color for plot areafont_color(str): Font color
Returns:
plotly.graph_objects.Figure: Modified figure with dark theme
Example:
fig = line_plot(df, x='Date', y='Sales')
fig = apply_dark_theme(fig)
fig.show()Apply custom layout settings to a figure.
Parameters:
fig(Figure): Plotly figure objecttitle(str, optional): Custom titlexaxis_title(str, optional): X-axis labelyaxis_title(str, optional): Y-axis labellegend_title(str, optional): Legend title
Returns:
plotly.graph_objects.Figure: Modified figure
Save a Plotly figure as an interactive HTML file.
Parameters:
fig(Figure): Plotly figure to savefilename(str): Output filename (e.g., "my_plot.html")notebook_name(str): Subfolder name in exports/html/
Output Location:
exports/html/{notebook_name}/{filename}
Example:
fig = line_plot(df, x='Date', y='Sales')
save_fig_as_html(fig, 'sales_trend.html', notebook_name='01_line_scatter')Save a Plotly figure as a static PNG image.
Requirements: Requires kaleido package
Parameters:
fig(Figure): Plotly figure to savefilename(str): Output filename (e.g., "my_plot.png")notebook_name(str): Subfolder name in exports/images/
Output Location:
exports/images/{notebook_name}/{filename}
Example:
fig = scatter_plot(df, x='Sales', y='Profit')
save_fig_as_png(fig, 'sales_vs_profit.png', notebook_name='01_line_scatter')Located in utils/streamlit_utils.py
Load and display a saved HTML plot in Streamlit.
Parameters:
file_path(Path or str): Path to HTML file
Example:
import streamlit as st
from pathlib import Path
from utils.streamlit_utils import load_html_plot
html_file = Path("exports/html/01_line_scatter/sales_trend.html")
load_html_plot(html_file)Advanced functions for adding statistical elements to plots.
Add a polynomial trendline to an existing figure.
Parameters:
fig(Figure): Existing Plotly figuredf(DataFrame): Source datax(str): X column namey(str): Y column nameorder(int): Polynomial order (1=linear, 2=quadratic, etc.)
Returns:
plotly.graph_objects.Figure: Figure with trendline added
Add a moving average line to a time series plot.
Parameters:
fig(Figure): Existing Plotly figuredf(DataFrame): Source datay(str): Column to calculate MA forwindow(int): Rolling window sizecolor(str): Line color
Returns:
plotly.graph_objects.Figure: Figure with MA line added
Add confidence bands based on z-score.
Parameters:
fig(Figure): Existing Plotly figuredf(DataFrame): Source datay(str): Column to calculate bands forz(float): Z-score multiplier (default: 2 for ~95% CI)color(str): Fill color for band
Returns:
plotly.graph_objects.Figure: Figure with confidence bands
Many utility functions return Figure objects, allowing for method chaining:
fig = (line_plot(df, x='Date', y='Sales', title='Sales Trend')
.pipe(apply_dark_theme)
.pipe(add_moving_average, df=df, y='Sales', window=7))
save_fig_as_html(fig, 'sales_with_ma.html')from utils.plot_utils import quick_preview
quick_preview(df, chart_type='scatter', x='Sales', y='Profit', color='Category')For better IDE support, functions use type hints:
from typing import Optional
import pandas as pd
import plotly.graph_objects as go
def line_plot(
df: pd.DataFrame,
x: str,
y: str,
color: Optional[str] = None,
title: str = "",
markers: bool = True,
template: str = "plotly_white"
) -> go.Figure:
...All utility functions include basic error handling:
- Missing columns: Raises
KeyErrorwith helpful message - Invalid data types: Raises
TypeError - Empty DataFrames: Raises
ValueError
Example:
try:
fig = line_plot(df, x='NonExistentColumn', y='Sales')
except KeyError as e:
print(f"Column not found: {e}")For more examples, see the Jupyter notebooks in the notebooks/ directory.