liveBook · Manning

Dropdown

As explained in Data Storytelling with Altair and AI

A dropdown menu is an interactive control that reveals a list of choices from which a user can select an option.

A drop-down menu is an interactive control that reveals selectable options. In data visualization, it lets audiences explore alternatives while keeping the chart’s main message focused. section 8.4 section 8.4.3

Build a drop-down menu in Altair through four connected steps:

  • Create a binding_select() object containing the available values. section 8.4.3
  • Create a selection_point() object to govern the menu-chart interaction. section 8.4.3
  • Attach the selection to the chart with add_params(). section 8.4.3
  • Use transform_filter() to display only the selected data. section 8.4.3

Keep the story focused

A chart that lets viewers choose any reference country supports exploration but lacks a clear focus. A stronger design keeps North America static and lets viewers select another country for comparison. section 8.4 section 8.4.4

Listing 8.6 Setting a drop-down menu
import pandas as pd
import altair as alt

df = pd.read_csv('data/population.csv')

# [...] 
color = '#80C11E'
input_dropdown = alt.binding_select(options=df['Country Name'].unique())
select_country = alt.selection_point(name='Select',fields=['Country Name'], bind=input_dropdown, value=[{'Country Name': 'Africa Eastern and Southern'}])


# Create visualization
chart = alt.Chart(df).mark_line(
    color=color
).encode(
        y=alt.Y('Population:Q', title=''),
        x=alt.X('Year:O'),
).add_params(
    select_country
).transform_filter(
    select_country
)

# [...]
chart.save('dropdown.html')

Inspect the complete Altair pattern using population data, a selectable country list, a default value, and filtered line-chart output. section 8.4.3

How Data Storytelling with Altair and AI covers it

How deep it goes
Details four Altair implementation steps and stops short of broader interaction design beyond the exercise examples.
Examples
Uses population-by-continent lines, North America comparisons, country selection, and positive-versus-negative trend highlighting.
What it assumes
Assumes familiarity with charts and data but teaches the menu’s Altair construction sequence explicitly.
What you can do after
Build it from scratch in Altair by binding selectable values, attaching a selection, and filtering chart data.
How it connects
Connects Altair with interactive charts, selection, filtering, sliders, tooltips, and data visualization.

Also explained in

Compare how 10 books explain dropdown →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Storytelling with Altair and AI. The section links point to the parts of the book it draws on.