Skip to content
Tutorials Center
My learning

Topics

Lesson 41 of 42 ยท Projects and Practice

Project: A World Data Explorer

Build an interactive world data explorer with Plotly: animated choropleth, metric dropdown, bubble scatter, top-10 bars and a Dash-ready structure using the gapminder data.

This project combines maps, animation, dropdowns and statistical summaries into a small "world explorer". The user picks a measure (life expectancy, population, income) and a year, and sees a map, a ranking and a relationship chart. You will build the pieces as plain Plotly figures first; at the end you will see how the same functions plug into a Dash app.

The data is the gapminder sample that ships with Plotly: 142 countries, every fifth year from 1952 to 2007, with life expectancy, population and GDP per person. It is a teaching dataset (not current), and ideal for practice.

Step 1: explore and prepare

Before drawing anything, get to know the table: columns, ranges, missing values. Then compute extra measures you will need, such as total GDP and the change over time.

Python Step 1: look at the data and derive measures

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 dataset contains iso_alpha (three-letter code) and iso_num columns, ready for map joins. The "smallest gains" show that some countries went backwards, a story worth a chart.

Step 2: an animated map

A choropleth with animation_frame="year" shows how the world changes. Fix range_color so colors mean the same thing in every frame.

Python Step 2: choropleth over time

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.

All 12 frames contain a full set of countries, so the figure is not tiny, but a choropleth is a good case for animation: the position of each region never changes, only the color.

Step 3: a metric switcher without a server

Instead of animating the map, you may prefer a dropdown that switches the measure. All the metrics are present in the figure as separate traces, and only one is visible at a time, as in dropdown menus and buttons. Here is the pattern with a small hand-written dataset in Plotly.js, using the update method to change both the data (z) and the title:

Step 3: a metric dropdown on a world map (Plotly.js)

The numbers in this inline table are rounded and illustrative for demonstration only; use real, sourced data for any published chart, and note the source and year on it.

Step 4: the supporting charts

A map shows where, but readers also want which are the top, and how do measures relate. A horizontal bar chart of the top 10 and a bubble scatter for a chosen year complete the explorer. Write each as a function of the year and the metric: that is exactly the shape a callback needs.

Python Step 4: figure-building functions for any year

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.

Because each function takes only simple inputs (year, metric) and returns a figure, you can call it from anywhere: a notebook cell, a script that saves HTML, or a Dash callback. Keeping figure construction in functions is the most useful habit in this whole course.

Step 5: assemble and share

Static sharing is simple: build the figures and write one HTML page.

Python
from plotly.io import to_html

parts = [to_html(f, full_html=False, include_plotlyjs="cdn" if i == 0 else False)
         for i, f in enumerate([map_fig, top_bar(2007), bubble(2007)])]
open("world.html", "w", encoding="utf-8").write("<h1>World explorer</h1>" + "".join(parts))

For live filters (a year slider, a continent checklist), use Dash. The layout holds a dcc.Slider(id="year", min=1952, max=2007, step=5), a dcc.Dropdown(id="metric", ...) and three dcc.Graphs; a callback with Input("year", "value") and Input("metric", "value") returns top_bar(year, metric) and bubble(year). Nothing else changes.

flowchart LR
    S["Year slider + metric dropdown"] --> C["Callback"]
    C --> F1["top_bar(year, metric)"]
    C --> F2["bubble(year)"]
    C --> F3["map figure"]
    F1 --> G["dcc.Graph x3"]
    F2 --> G
    F3 --> G

Figure functions stay the same; only the inputs come from the page.

Design checklist

  • Say what the color means in the colorbar title, with units.
  • Fix color and axis ranges when the user can change the year, so changes are real changes.
  • Log axes for money and population, because they span several orders of magnitude.
  • Cite the source and the year in an annotation at the bottom: xref="paper", yref="paper", y=-0.15.
  • Test on a phone. Maps and big legends need margin and legend tweaks on a narrow screen.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should range_color be set for an animated choropleth?

    Without it, each frame may rescale its colors and the map would seem to change when it did not.
  2. 2. What is the advantage of writing figures as functions of (year, metric)?

    A function with simple inputs and a figure output fits every environment unchanged.
  3. 3. In a metric dropdown that changes z, which method changes data and title together?

    update accepts trace changes and layout changes in one call.
  4. 4. Why use a log x-axis for GDP per person?

    Incomes range from a few hundred to tens of thousands. A log axis keeps the poorer countries readable.

Summary

  • Explore the data first, then derive the measures you need (totals, changes).
  • Animated choropleths need fixed color ranges; dropdowns switch metrics without a server.
  • Top-N bars and bubble scatters complement the map.
  • Write figure functions of simple inputs so they work in notebooks, HTML files and Dash.
  • Cite sources and use log axes for quantities that span orders of magnitude.

Next: Plotly Practice and Common Pitfalls