Visualization
Intermediate 14 min read Visualization

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.

plotlygoexpresshoverinteractiveshare
Prerequisites: Matplotlib Basics
Before we start

What you will learn

  • Express charts from dataframes with plotly
  • Add hover, zoom and animation
  • Publish figures to the web
Introduction

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.

Motivation

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.
Think about it

Mental model

Imagine

A paper map vs a GPS app you pinch, zoom and tap for details

Think of

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.

Mechanics

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.

DataFrame
px / go
Figure
Render
Consume
Hands-on

Examples

Express scatter with hover

Express scatter with hover
import plotly.express as px
import pandas as pd
df = 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 file
print("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.

Real world

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.
Guidelines

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.
Watch out

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.
When things break

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.
Interviews

Interview checkpoint

Practice

Hands-on lab

From Static to Interactive in Minutes
25 min · intermediate
In progress
Objectives
  • 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

0/5 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

A plotly fig.write_html('f.html') creates:

2

Plotly figures must re-run Python to respond to a viewer's zoom.

Go deeper

Official documentation