Interactive Visualizations
Charts you can interrogate
Plotly turns dataframes into hoverable, zoomable web charts with the same API breadth as matplotlib — the fastest path from notebook to shareable dashboard.
What you will learn
- Express charts from dataframes with plotly
- Add hover, zoom and animation
- Publish figures to the web
What is this?
Plotly's express API (px) builds interactive figures from dataframes with one call; go provides lower-level control. Figures render as HTML/JS in notebooks or via plotly dashboards/JSON, giving hover tooltips, zoom, pan, range sliders and links. The dataframe-to-plotly match makes it the standard for exploratory and shareable visuals.
Why do you need it?
- Hover inspectively reads exact values — a killer feature for exploratory charts.
- Web/HTML output shares without a viewer needing Python installed.
- geo and 3D render natively — city-level choropleths, surfaces.
- Dashboards built on plotly (via Dash) stay in the same language as research.
Mental model
A paper map vs a GPS app you pinch, zoom and tap for details
Static matplotlib vs interactive plotly
Both show the same world; the interactive one lets users ask 'what exactly is that?' without re-running code. The map doesn't redraw pages; the chart doesn't regenerate for hover.
How it works
- fig = px.scatter(df, x='a', y='b', color='group', hover_data=['id']).
- Interactivity: hover tooltips from column names, zoom by wheel, pan by drag, toggles in legend.
- go.Figure(go.Bar(...)) gives explicit low-level control for multi-layer figures.
- fig.show() renders in notebook; fig.write_html('f.html') produces a self-contained file.
- Layout: fig.update_layout(title=..., template='plotly_white') rescales styling.
The plotly stack.
Examples
Express scatter with hover
import plotly.express as pximport pandas as pddf = pd.DataFrame({ "spend": [10, 200, 150, 80], "revenue": [100, 550, 700, 260], "tier": ["free","pro","pro","free"], "user": ["u1","u2","u3","u4"],})fig = px.scatter(df, x="spend", y="revenue", color="tier", hover_data=["user"], title="Spend vs Revenue")fig.write_html("spend.html") # self-contained shareable fileprint("done")One call yields a hoverable, zoomable HTML chart. The file is self-contained JavaScript — open it in any browser without a running Python process.
In production
- A growth team ships plotly HTML into a company wiki; PMs interrogate cohort hover without notebooks.
- A feature-flag team overlays experiment groups onto time-series charts with range sliders.
- A geo-marketing team choropleths store-level revenue with labels on hover.
Best practices
- Pass hover_data with the ID keys analysts need.
- Use template='plotly_white' and update_layout for clean defaults.
- write_html for sharing; keep JSON (to_json) for storage.
- Limit to ~50k points or aggregate — thousands of hover markers hurt.
Common mistakes
- Plotting 1M points → frozen viewer; aggregate or sample.
- Leaving default hoover off for wide dataframes (bloats tooltips).
- Treating interactive charts as the export point when the report needs static PNGs.
Debugging tips
- Hover shows 'NaN'? Confirm the hover_data column is numeric/null-corrected.
- Large frame stalls? Reduce with df.sample(20000) or aggregate.
- If write_html doesn't open offline, use include_plotlyjs and open the local file.
Interview checkpoint
Hands-on lab
- Build an express scatter with hover
- Layer lines with go for a time series
- Export to self-contained HTML
From Static to Interactive in Minutes
25 min · intermediate
Quick quiz
A plotly fig.write_html('f.html') creates:
Plotly figures must re-run Python to respond to a viewer's zoom.