Plotly — Interactive Charts
plotly.express), the high-level module that mirrors Seaborn's "point it at a DataFrame" style, to build bar, line, and scatter charts, then export one to a standalone HTML file.
Why Interactive Charts?
A static chart shows one fixed view of the data — if a bar's exact value matters, the viewer has to guess from the axis gridlines. An interactive chart lets the viewer explore it themselves.
plotly.express and px.bar()
plotly.express, conventionally imported as px, is Plotly's high-level charting API — one function call per chart type, taking a DataFrame and column names, much like Seaborn.
import plotly.express as px import pandas as pd df = pd.DataFrame({ "month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"], "revenue": [420000, 388000, 455000, 470000, 512000, 498000], }) fig = px.bar(df, x="month", y="revenue", title="Monthly Revenue") fig.show()
px.line()
px.line() follows the same pattern as px.bar() — same DataFrame, same x/y arguments — just connected with a line instead of bars, the natural choice for a trend over time.
fig = px.line(df, x="month", y="revenue", markers=True, title="Revenue Trend") fig.show()
px.line() draws just the connecting line. markers=True adds a visible dot at each actual data point, which makes it much easier to hover over an exact month rather than a spot along the line between two months.px.scatter()
px.scatter() adds a few arguments beyond what Matplotlib and Seaborn offer directly: color for categorical grouping, size to scale points by a numeric column, and hover_name to control what label appears in the tooltip.
products = pd.DataFrame({
"product": ["Laptop", "Mouse", "Monitor", "Keyboard", "Webcam"],
"price": [1200, 45, 890, 65, 120],
"units_sold": [120, 340, 90, 210, 150],
"category": ["Electronics", "Accessories", "Electronics", "Accessories", "Electronics"],
})
fig = px.scatter(
products, x="price", y="units_sold",
color="category", size="units_sold",
hover_name="product",
title="Price vs. Units Sold",
)
fig.show()
hue to color points by category, Plotly Express uses color for the same job — both add a matching legend automatically. The names differ between the two libraries, which is worth remembering when switching back and forth.fig.show()
Every px.*() function returns a Figure object — nothing is drawn until you call .show() on it. In a Jupyter notebook it renders inline as an interactive widget; in a plain script it opens in your default web browser.
Figure, a Plotly figure is a completely different object from Matplotlib's — ax.set_title() and plt.show() don't apply here. Plotly figures use their own methods, like fig.update_layout(title="...") for further customization, which goes beyond what this lesson covers but is worth knowing exists.Exporting to a Standalone HTML File
Because a Plotly chart is fundamentally a small web app (HTML + JavaScript), it can be saved as one — a single .html file that keeps its hover, zoom, and pan behavior when opened in any browser, with no Python installation needed to view it.
fig = px.line(df, x="month", y="revenue", markers=True, title="Revenue Trend") # Writes a self-contained interactive HTML file — open it in any browser fig.write_html("revenue_trend.html")
fig.write_html() keeps the chart fully interactive. If a plain static image is needed instead — for a slide deck or a printed report — fig.write_image("chart.png") is the equivalent, though it requires the extra kaleido package to be installed to render to a raster image.Lesson Summary
Let's recap everything you learned in this lesson:
px) is the high-level API — px.bar(), px.line(), px.scatter() all take a DataFrame directly.px?px.scatter(df, x="price", y="units_sold", size="units_sold"), what does size control?Build two interactive charts from the df and products DataFrames used in this lesson.
Using
df (month, revenue) and products (product, price, units_sold, category): (1) build a px.line() trend chart of revenue by month with markers=True, (2) build a px.scatter() chart of price vs. units_sold, colored by category and sized by units_sold, with hover_name="product", and (3) export just the line chart to "monthly_revenue.html".
Rules: Call
fig.show() on both charts, and give both a title=.
💡 Show hints if you're stuck
- Step 1:
px.line(df, x="month", y="revenue", markers=True, title="Monthly Revenue Trend") - Step 2:
px.scatter(products, x="price", y="units_sold", color="category", size="units_sold", hover_name="product", title="Price vs. Units Sold") - Step 3:
fig.write_html("monthly_revenue.html")— call this on the line chart's figure before or after fig.show()