Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 39 ยท Widgets and Visualization

Interactive Charts and Progress Bars

Go beyond static plots in Jupyter with Plotly, Altair and Bokeh, learn how chart specifications travel to the browser, and show progress for long loops with tqdm.

Static images answer a question once. Interactive charts let the reader hover for exact values, zoom into a crowded region, toggle series on and off and filter by selection, so one chart can answer many questions. In this lesson you meet the three most popular interactive libraries for notebooks, see the idea that makes them work (a chart is data plus a specification, rendered in the browser by JavaScript), and learn to show progress bars for long computations.

These libraries draw in the browser through JavaScript and need a notebook front end, so their code is read-only here. The stand-ins build the same kind of chart specification and the same kind of progress line in plain Python, so the concepts are runnable.

Why interactive?

Hovering shows the exact number behind a bar; zooming reveals a pattern inside a dense scatter plot; a legend click hides a distracting series. For exploration this saves you from writing five different plots. For communication it lets a reader ask their own questions. The trade-off: interactive charts are heavier (they embed data and JavaScript), and a PDF export shows only a static image.

Plotly

Plotly has a high-level interface, plotly.express, that makes a chart from a DataFrame in one line:

Python
import plotly.express as px

fig = px.scatter(
    df, x="area_km2", y="population_m",
    color="country", size="density", hover_name="city",
    title="Cities: area against population",
)
fig.show()

In JupyterLab 4 and Notebook 7 this works out of the box; hover shows values, a toolbar offers zoom, pan, box select and "save as PNG". Other one-liners: px.line, px.bar, px.histogram, px.box, px.choropleth (maps), px.imshow, and px.scatter_3d. Customise afterwards with fig.update_layout(...) and fig.update_traces(...). Static export (PNG, PDF) needs the extra kaleido package.

Altair (Vega-Lite)

Altair is declarative: you describe how data columns map to visual channels (position, colour, size) and Altair produces a Vega-Lite specification, a JSON document that the browser renders:

Python
import altair as alt

chart = (
    alt.Chart(df)
    .mark_circle()
    .encode(x="area_km2", y="population_m", color="country", tooltip=["city", "density"])
    .interactive()
)
chart

.interactive() adds zoom and pan; alt.selection_interval() and alt.condition(...) link several charts so selecting in one filters another. Because the output is plain JSON plus a small viewer, Altair charts survive export to HTML.

Bokeh and others

Bokeh builds interactive charts with its own server and widgets; it is a good choice when you want dashboards with callbacks in Python. Other tools worth knowing: HoloViews/hvPlot (a very concise syntax over Bokeh), pygwalker (drag-and-drop exploration of a DataFrame), and ipyleaflet or folium for maps. Choose by need: Plotly for general charts and 3D, Altair for statistical graphics with a clean grammar, Bokeh and HoloViews for dashboards.

A chart is data plus a specification

The reason these charts are exportable and shareable is that the notebook output is a MIME bundle (see Rich output and display) with a special type, for example application/vnd.vegalite.v5+json or application/vnd.plotly.v1+json. The front end (JupyterLab) has a renderer for that type and draws the chart. Your Python code only builds the description.

Here is a stand-in that builds a Vega-Lite-style specification by hand from a list of records and checks it. This is the same thing Altair does when you call .encode(...):

Python Stand-in: build a chart specification as JSON

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The data travels inside the specification, which is why a big DataFrame makes a heavy notebook: Altair even refuses by default to embed more than 5,000 rows. Aggregate or sample before charting, or use a data transformer that writes the data to a file.

Static plus interactive

A sound pattern for notebooks you share: draw the key charts with Matplotlib (static, always visible, see Plots in notebooks) and offer interactive versions for exploration in an appendix. If your readers will open the notebook in a viewer that does not run JavaScript (some code-hosting previews), interactive output may not appear at all.

Progress bars with tqdm

Long loops feel broken if nothing moves. tqdm shows a progress bar with elapsed time and an estimate of the remaining time. In a notebook use the notebook flavour, which draws a graphical bar:

Python
from tqdm.notebook import tqdm
import time

for item in tqdm(range(100), desc="Processing"):
    time.sleep(0.05)

tqdm.auto picks the best version for the environment (widget bar in a notebook, text in a terminal), which makes your code portable:

Python
from tqdm.auto import tqdm
results = [work(x) for x in tqdm(items)]

Useful options: total= when the iterable has no length, leave=False to remove the bar afterwards, and tqdm.pandas() followed by df["col"].progress_apply(func) for pandas.

Under the hood a progress bar needs only the number done, the total and the start time. This stand-in prints text bars computed exactly as tqdm does, using a fake clock so the output is deterministic:

Python Stand-in: a tiny progress bar

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In a real notebook, the bar updates in place, using the same output-replacement technique as display_id handles.

Responsiveness tips

  • Keep interactive charts under a few thousand points; aggregate or bin larger data first (hexbin, datashader for millions of points).
  • Set a clear title and axis labels; hover text is not a substitute.
  • Choose colour palettes that work for colour-blind readers (viridis-like, or Plotly's px.colors.qualitative.Safe).
  • Test the notebook after "Restart and Run All" to be sure every chart is created by the code, not by leftovers (see Execution order and hidden state).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Altair produce when you build a chart?

    Altair describes the chart declaratively as Vega-Lite JSON, and the front end draws it.
  2. 2. Why can interactive charts make a notebook file large?

    The data travels inside the output so the chart can be drawn without the kernel.
  3. 3. Which import gives a progress bar that picks the right version for notebook or terminal?

    tqdm.auto selects the widget bar in notebooks and the text bar elsewhere.
  4. 4. What is a downside of interactive charts in a PDF export?

    PDF has no JavaScript, so interactivity is lost and sometimes the chart is blank.

Summary

  • Interactive charts add hover, zoom, selection and filtering; Plotly, Altair, Bokeh and HoloViews are the main choices.
  • Charts are data plus a specification that the front end renders from a special MIME type.
  • Big datasets make heavy notebooks; aggregate, sample or bin first.
  • Provide a static version of key charts for exports and previews that cannot run JavaScript.
  • tqdm (preferably tqdm.auto) shows progress and an estimated time for long loops.
  • Verify with Restart and Run All that charts are reproducible.

Next: Reproducible Environments for Notebooks