Adding Interactivity

Interactivity enables our audience to engage with the chart and explore the data on a whole new level. It ranges from tooltips providing additional information when hovering over a point to selections allowing users to filter and highlight specific areas of interest.

There are two main types of interactivity:

  • Passive interactivity refers to features that allow the audience to interact with the chart without changing its state, such as tooltips or hover effects.

  • Active interactivity involves changing the chart’s state in response to user input.

Within these two categories, we can add interactivity to our Altair charts in several ways.

  • Use sliders or drop-down menus to filter data based on specific criteria or range selection tools for highlighting certain chart parts.

  • Add clickable elements, such as buttons, icons, triggers actions, or updates within the chart.

Adding a tooltip

A tooltip is a tiny pop-up box that appears when we hover over certain chart elements, such as data points or bars. To add a tooltip to our Altair chart, we can specify which element(s) we want it to appear for. In Altair, we can do it through the tooltip encoding channel. Next, we define what information should be displayed in the tooltip using square braces [].

Consider the following example, which includes all the DataFrame columns in the tooltip:

Get hands-on with 1200+ tech skills courses.