Why Plotly?
Matplotlib and Seaborn draw static pictures — great for reports and papers, but a reader can't poke at them. Plotly draws figures that respond to the mouse:
- Hover tooltips (see the exact value under the cursor)
- Zoom and pan (drag to inspect a busy region)
- Legend toggling (click a series name to hide/show it)
This makes Plotly great for:
- Exploratory analysis (you dig into the data yourself)
- Sharing insights in a browser (no extra software needed)
- Lightweight dashboards (a handful of linked charts on one page)
Plotly Express vs Graph Objects
Plotly is really two APIs layered on top of each other:
plotly.express(px) — a high-level, "one function per chart type" API. You hand it a tidy DataFrame and column names; it builds the whole figure. Start here for almost everything.plotly.graph_objects(go) — the low-level building blocks (go.Scatter,go.Bar, ...) thatpxitself is built from. Reach for it when you need fine controlpxdoesn't expose, or when combining traces manually (see subplots/dashboards later in this phase).
This phase focuses on Plotly Express, because it covers the vast majority of everyday charts with the least code.
Anatomy of a figure
Every Plotly chart is a figure: a Python object holding
- data — one or more traces (a line, a set of bars, a scatter layer...)
- layout — title, axis labels, legend position, margins
- config — export/interaction options (zoom mode, static vs interactive)
px.line(...), px.bar(...), px.scatter(...) etc. all return this same kind of figure object — which is why they share methods like .show(), .update_layout(), and .write_html().
First interactive chart
import pandas as pd
import plotly.express as px
df = pd.DataFrame({
"day": [1, 2, 3, 4, 5],
"orders": [120, 140, 130, 160, 155],
})
fig = px.line(df, x="day", y="orders", title="Orders over time")
fig.show()Hover over any point in the rendered chart and Plotly shows you the exact day/orders values — no extra code required.
Saving/exporting
You can export a figure two very different ways:
- HTML — best for interactive sharing (keeps hover, zoom, legend toggling)
- PNG/JPEG/SVG — static image, good for slides or a printed report
fig.write_html("orders.html")fig.write_image("orders.png") # requires the `kaleido` packagePNG/SVG export needs the extra kaleido dependency installed (pip install -U kaleido); HTML export needs nothing beyond Plotly itself.
flowchart LR A["Tidy DataFrame"] --> B["px.line / px.bar / px.scatter ..."] B --> C["Figure object
(data + layout)"] C --> D{"fig.show() or export?"} D -- "Explore now" --> E["fig.show()
interactive in browser/notebook"] D -- "Share/save" --> F["fig.write_html()
or fig.write_image()"]
Check your understanding
What is the main difference between Plotly and Matplotlib or Seaborn in this lesson?
Plotly figures support hover, zoom and pan, which static images lack.
Show answer
B — Plotly figures respond to the mouse, while the others draw static pictures — Plotly figures support hover, zoom and pan, which static images lack.
Which export needs the extra kaleido package?
PNG and SVG export need kaleido, while HTML export needs only Plotly itself.
Show answer
B — fig.write_image to PNG — PNG and SVG export need kaleido, while HTML export needs only Plotly itself.
What do px.line, px.bar and px.scatter all return?
They all return a figure, which is why they share the same methods.
Show answer
B — The same kind of figure object with methods like show and update_layout — They all return a figure, which is why they share the same methods.
Which pair are the two Plotly APIs layered on top of each other?
Plotly Express is the high-level API and Graph Objects gives lower-level control.
Show answer
A — Plotly Express and Graph Objects — Plotly Express is the high-level API and Graph Objects gives lower-level control.
Next
Continue to: Interactive Line Charts to build trend charts with markers and multiple series.
🧪 Try It Yourself
Exercise 1 – Build Your First Figure
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Choose the Right API
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Export for Sharing
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.