Static charts are great for academic papers, but modern business stakeholders demand interactivity — the ability to hover for exact values, zoom into outliers, and isolate specific categories. Plotly Express brings the interactive power of modern web standards (D3.js & WebGL) into concise Python syntax.
import plotly.express as px
import pandas as pd
import numpy as np
# Sample customer lifetime value dataset
np.random.seed(42)
n_users = 300
df = pd.DataFrame({
'user_id': [f"USR_{i:04d}" for i in range(n_users)],
'annual_spend': np.random.exponential(scale=1200, size=n_users) + 150,
'session_count': np.random.poisson(lam=45, size=n_users),
'nps_score': np.random.randint(1, 11, size=n_users),
'subscription_tier': np.random.choice(['Free', 'Pro', 'Enterprise'], size=n_users, p=[0.6, 0.3, 0.1])
})
# Create interactive scatter plot with hover tooltips, color encoding, and size scaling
fig = px.scatter(
df,
x='session_count',
y='annual_spend',
color='subscription_tier',
size='annual_spend',
hover_name='user_id',
hover_data={'nps_score': True, 'annual_spend': ':.2f'},
labels={'session_count': 'Total App Sessions', 'annual_spend': 'Annual Revenue ($)'},
title="Customer Lifetime Value: Engagement vs. Annual Spend",
color_discrete_map={'Free': '#94a3b8', 'Pro': '#38bdf8', 'Enterprise': '#F48FB1'},
template='plotly_white'
)
# Enhance layout with zoomable range slider
fig.update_layout(
font_family="Segoe UI, Roboto, sans-serif",
title_font_size=18,
hoverlabel=dict(bgcolor="white", font_size=12)
)
# Export as standalone interactive HTML
fig.write_html("customer_clv_interactive.html")
fig.show()
Plotly can render worldwide choropleth maps with one function call: px.choropleth(df, locations='country_iso', color='gdp_growth', hover_name='country'), rendering responsive SVG maps that seamlessly embed into executive dashboards.
Broaden your statistical analysis, Python data wrangling, and production engineering expertise:
Compare static publication charts with dynamic browser visualizations.
Create interactive geographical maps and choropleths with Plotly Express.
Build real-time analytical dashboards powered by Plotly charts.
Feed browser-computed SQL results directly into Plotly JavaScript charts.