Interactive Charts with Plotly
Build hoverable, zoomable Plotly charts for exploration and stakeholder demos.
In plain words
Plotly makes charts you can hover over, zoom into and click, so people can explore the numbers themselves.
Think of it like…
A paper map versus a digital map: the digital one lets you zoom, tap a point for details and toggle layers, but it needs a device, and if every layer is on at once it becomes harder to read than the paper version.
Where you’ve seen it
A marketing analyst plots 2,400 campaigns: fig = px.scatter(df, x="spend", y="signups", color="channel", size="budget", hover_data=["campaign", "cpa"], log_x=True, trendline="ols"). Stakeholders hover to see each campaign's cost per acquisition, double-click a legend entry to isolate a channel and box-select a cluster of low-efficiency campaigns. The figure is saved with fig.write_html("campaigns.html", include_plotlyjs="cdn") (a few hundred kilobytes, since the plotly.js bundle loads from a CDN rather than being embedded) and shared by email. When the team tries the same chart on 800,000 ad impressions, the default SVG rendering lags, so they switch to WebGL (render_mode="webgl") and then bin the data with a density heatmap because embedding 800,000 points would also make the HTML around 15 MB.
Words you’ll hear
- Plotly Express
- High-level API that builds a figure from a DataFrame in one function call.
- Hover template
- A format string controlling exactly what the tooltip shows.
- WebGL rendering
- GPU-based drawing (scattergl) that handles far more points than SVG.
- Dash / Streamlit
- Frameworks that turn Plotly figures into interactive web apps with callbacks.
Visual diagram
Theory & deep dive
Building and customising figures
Plotly Express covers the common chart types with one function each (scatter, line, bar, histogram, box, violin, heatmap/imshow, choropleth, treemap, sunburst, funnel) and maps DataFrame columns to x, y, color, size, symbol, facet_col, facet_row, animation_frame and hover_name/hover_data. It returns a Figure you can refine: fig.update_traces(hovertemplate="%{x}<br>CPA: $%{customdata[0]:.2f}<extra></extra>"), fig.update_layout(title=..., hovermode="x unified", template="plotly_white"), fig.add_vline and fig.add_annotation for events, fig.update_xaxes(rangeslider_visible=True) for time series. Use go.Figure and make_subplots when you need mixed trace types, secondary axes or custom layouts.
Good interactive design keeps the default view self-explanatory: a clear title, sorted categories, a sensible axis range and a highlighted message, with interactions adding depth rather than being required. Use hover for exact values and context fields (ids, units), legend clicks to isolate series, and range sliders or buttons for time windows. Avoid hover-only critical information (it fails on touch devices and in print), keep tooltip text short and formatted (thousands separators, 2 decimals) and test colours for accessibility as with static charts.
px.scatter(df, x="spend", y="signups", custom_data=["cpa"]) then fig.update_traces(hovertemplate="Spend $%{x:,.0f}<br>Signups %{y:,}<br>CPA $%{customdata[0]:.2f}<extra></extra>") shows only the three numbers that matter, formatted for reading.
Performance, sharing and when to use something else
Plotly figures embed their data as JSON in the page, so file size grows with the number of points: roughly 10 MB for 500,000 two-column points, plus the plotly.js library (several megabytes) unless loaded from a CDN with include_plotlyjs="cdn". The default SVG renderer slows noticeably beyond tens of thousands of points; WebGL traces (px.scatter(render_mode="webgl"), go.Scattergl) handle hundreds of thousands. Beyond that, aggregate before plotting: bin to a 2D density heatmap, resample time series, or use Datashader or server-side aggregation in Dash so the browser receives pixels or summaries rather than raw rows.
Sharing options: a self-contained HTML file for email or intranet, an embedded figure in a notebook or Quarto report, static PNG, SVG or PDF through Kaleido for documents (interactions are lost, so choose a view that works statically), or a hosted Dash or Streamlit app with filters (callbacks recompute figures, and authentication and deployment become your concern). Plotly isn't always the right choice: BI tools suit governed dashboards with scheduled refreshes, Matplotlib or Seaborn suit print figures, and Altair suits declarative grammar fans. Protect sensitive data: an HTML file contains every plotted value, so anyone with the file has the data.
800,000 impressions as individual points: a ~15 MB HTML file that lags. Binned into a 100 × 100 grid (px.density_heatmap(df, x="hour", y="bid", nbinsx=100, nbinsy=100)) the file is a few hundred KB, loads instantly and shows the density pattern more clearly.
How it works, step by step
Step 1 of 41. Start from tidy data
Prepare a long-form DataFrame with the columns you want for axes, colour, size and tooltips.
- Check
- row count and sensitivity
When to use it & trade-offs
What it is
Plotly is a Python (and JavaScript) charting library that renders interactive figures in the browser: hover tooltips, zoom and pan, legend toggling, selection, range sliders and animation. Plotly Express (px) builds a complete chart from a tidy DataFrame in one call (px.scatter(df, x="spend", y="signups", color="channel", hover_data=["campaign"])), while plotly.graph_objects (go) gives full control over traces and layout. Figures can be embedded in notebooks, exported as standalone HTML, turned into static images, or served as interactive apps with Dash or Streamlit.
Why it matters: Exploration and stakeholder demos benefit from letting people hover to see exact values, zoom into a spike or filter a series without asking an analyst to regenerate a chart. Interactivity also lets one chart carry more detail without clutter, because secondary information lives in the tooltip. The trade-offs are file size, performance on large data and the need to design interactions deliberately so the default view still tells the story.
When to reach for it
- Exploratory analysis and stakeholder demos
- Time series with zoom and range selection
- Lightweight internal apps with Dash or Streamlit
When to avoid or be careful
- Embedding hundreds of thousands of raw points in an HTML file
- Sharing HTML files that contain confidential rows
- Relying on hover for information that must be seen in print
Choosing a charting approach
Strengths: Hover, zoom and facets in a few lines; large chart range.
Watch out: Heavy files with big data; app hosting is on you.
Strengths: Fine control; light output.
Watch out: No built-in interactivity.
Strengths: Security, scheduling and drag-and-drop.
Watch out: Less flexible for custom analysis.
Interview signals & numbers to know
When this topic comes up
- “Let stakeholders explore the chart”
- “Hover to see values”
- “The chart is slow with a million points”
- Plotly Express and Dash
- WebGL scatter
Benchmarks & rules of thumb
- ~10 MB
- approximate JSON size for 500,000 two-column points
- tens of thousands
- points where SVG rendering starts to lag
- a few hundred KB
- HTML size when plotly.js is loaded from a CDN
Common mistakes & follow-ups
Common interview mistakes
- Making the chart unreadable until the viewer hovers over everything.
- Emailing an HTML file with customer-level data.
- Using SVG scatter on a million points and blaming the browser.
Likely interviewer follow-ups
- How would you make a Plotly chart handle a million points?
- When would you choose Dash instead of a static report?
- What should the default view of an interactive chart contain?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Interactive Charts with Plotly with data science interview Q&A and hands-on practice.