seaborn and interactive charts · lesson 8 of 10
Parts in this lesson
Aimport plotly.express as pxPlotly's quick, table-first interface
Bpx.scatter(df, x="a", y="b", color="g", hover_data=[...])a chart that shows rows on hover
Cpx.bar(..., animation_frame="week", range_y=[0, 3600])one frame per week, on one axis
Dfig.write_html("chart.html", include_plotlyjs="cdn")a chart as a web page

Interactive charts with Plotly

Last updated

A picture of a chart shows what you chose to show. An interactive chart lets the reader ask their own questions: hover over a point to see its whole row, zoom into a crowded corner, hide a group, play the weeks forward. Plotly makes charts like that from a pandas table in one line. In this lesson you'll make hover charts, explore with zoom and select, animate weekly sales with an honest axis, and save a chart as a web page.

By the end of this lesson you can

  • Make an interactive chart with plotly.express and choose what hovering shows
  • Use zoom, select and the legend to explore a chart
  • Animate a chart over time with a fixed axis
  • Save a chart as an HTML file to share

Hover to see the row

seaborn and matplotlib draw pictures. Plotly draws charts in a web page, with JavaScript, so readers can interact with them. Its quick interface, plotly.express (usually imported as px), takes a tidy table and column names, just like seaborn. Run the cells, then move the mouse over the dots (or tap one).

hover.ipynb
  • px.scatter(penguins, x=..., y=..., color="species") makes a figure object. Like a DataFrame, it's shown when it's the last thing in a cell; fig.show() shows it from anywhere in a cell.
  • Hovering over a dot shows a box with its values. hover_name="species" puts the species in bold at the top, and hover_data=[...] adds more columns to the box, so each dot carries its whole row: island, sex, bill length and year. That's the strength of an interactive chart: the reader can check the one penguin that looks odd without asking you.
  • fig.update_layout(...) changes the figure's layout (axis titles, legend) and fig.update_traces(...) changes the drawn data (here, bigger, see-through markers).

In this page each Plotly chart is drawn inside its own sealed-off frame, so it can't reach the rest of the page; on your computer, it appears under the cell in JupyterLab or VS Code the same way.

Zoom, select and hide

Every Plotly chart comes with tools in the bar at its top right, which appears when you move over the chart. Run the cells and try them.

explore.ipynb
  • Zoom: drag across part of the chart to zoom in on it; double-click to zoom back out. In the histogram, zoom into where the species overlap.
  • marginal="box" adds a box plot per species above the histogram, sharing its x-axis. barmode="overlay" with opacity=0.6 draws the species' bars on top of each other, see-through.
  • The legend is a filter: click a species to hide it, double-click to show only that one. Every panel follows.
  • facet_col="island" makes one panel per island, like seaborn's col=. Choose the box select tool, drag over some points, and the rest fade, in every panel.
  • Pan, reset axes and more are in the same bar.

Interactivity helps readers explore; it doesn't replace a clear chart. A reader who never touches the chart should still get the point from the title and what's shown first.

Wrong way: an axis that doesn’t fit

animation_frame= makes one frame per value of a column, with a play button and a slider. Run the cells: each store's kilos sold so far this year, week by week, for the eight full weeks.

weeks.ipynb

The first cell adds a running total per store with cumsum and shows it: Batangas goes from 428 kg after the first week to 3,443 kg after the eighth. Now press play on the first chart and watch the y-axis. Plotly fits it to the first frame, which tops out a little over 400, and then keeps it. In the second week three of the four bars run off the top of the chart, and by the third week Rosario's does too: the reader can't see how big any of them got.

Animations go wrong with axes in two ways. If the axis stays fitted to the first frame, later bars spill over, as here. If a tool refits the axis to every frame, the bars stay inside but the scale jumps, so a bar can look the same height in week 2 as in week 8 even though its value has grown eight times. Either way the reader can't compare frames, which is the whole point of animating.

The second chart passes range_y=[0, 3600]: one axis, fixed, from zero, big enough for every frame. Now the bars grow week by week and a height means the same in week 1 as in week 8. The check: an animation is a set of small multiples shown one at a time (7.6), so it needs the same shared axes: set range_x and range_y to fit every frame.

Saving to HTML

A Plotly chart is a web page, so you can save it as one, email it, or put it on a website, and it stays interactive. Run the cells.

save.ipynb
  • fig.write_html("penguins.html", include_plotlyjs="cdn") writes a file of about 23 KB: the chart's data and settings, and a line that loads plotly.js, the JavaScript that draws it, from Plotly's server when someone opens the file.
  • By default (include_plotlyjs=True) the whole of plotly.js is put inside the file, nearly 5 MB, so it works with no internet connection. Choose by who'll open it.
  • The second cell looks inside: it's an ordinary HTML file.

To save a chart as an image instead (PNG, SVG or PDF for a report), Plotly uses a separate package, Kaleido: fig.write_image("penguins.png"). It isn't in this page's Python.

Check what you learned

You want each dot’s tooltip to show the store and the date as well. Which argument?
Why set range_y on an animated bar chart?
You’re sending a chart to a colleague who may open it offline. Which?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis