Skip to content

Add tooltip support to Altair backend#3392

Merged
quaquel merged 13 commits intomesa:mainfrom
annapurna-gupta:add_tooltip_to_altail_agent
Mar 2, 2026
Merged

Add tooltip support to Altair backend#3392
quaquel merged 13 commits intomesa:mainfrom
annapurna-gupta:add_tooltip_to_altail_agent

Conversation

@annapurna-gupta
Copy link
Copy Markdown
Contributor

@annapurna-gupta annapurna-gupta commented Feb 27, 2026

Fixes #2795

Summary

Add tooltip support to the Altair visualization backend so additional agent data can be shown on hover.

Motive

Enable showing extra agent information on hover in Altair visualizations. Earlier, there was no simple way to add this hover data.

Implementation

  1. Added an optional tooltip field to AgentPortrayalStyle so users can pass extra info to show on hover.

  2. Updated the Altair backend to collect this data, add it to the DataFrame, and display it using Altair tooltips.

  3. Added a check in the Matplotlib backend to raise an error if tooltip is used (since Matplotlib is not interactive).

  4. Updated an example and tests to show how the feature works.

Usage Examples

def agent_portrayal(agent): return AgentPortrayalStyle( color=agent.wealth, tooltip={"Agent ID": agent.unique_id, "Wealth": agent.wealth}, )

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.1% [-0.6%, +0.4%] 🔵 -0.2% [-0.5%, +0.1%]
BoltzmannWealth large 🔵 -1.2% [-1.8%, -0.6%] 🔵 +1.0% [+0.4%, +1.7%]
Schelling small 🔵 -0.6% [-0.9%, -0.3%] 🔴 +7.8% [+7.6%, +8.0%]
Schelling large 🔵 -0.2% [-0.8%, +0.4%] 🔴 +4.9% [+4.1%, +5.5%]
WolfSheep small 🔵 -0.2% [-0.5%, +0.1%] 🔵 +1.2% [+1.0%, +1.3%]
WolfSheep large 🔵 -0.6% [-1.3%, +0.1%] 🔵 +0.4% [-0.5%, +1.4%]
BoidFlockers small 🔵 +1.5% [+1.1%, +1.8%] 🔵 +0.9% [+0.8%, +1.1%]
BoidFlockers large 🔵 +0.1% [-0.2%, +0.4%] 🔵 +0.1% [-0.1%, +0.3%]

@SAKSHI-DOT693
Copy link
Copy Markdown

Thanks for the implementation — this is a nice addition to the Altair backend.

I had a small observation regarding the tooltip handling in draw_agents. We currently iterate over arguments["tooltip"] multiple times and perform per-row df.loc updates inside nested loops.

Since the Schelling benchmarks show a slight runtime increase, I’m wondering if the row-wise DataFrame mutation might be contributing.

It might be worth considering pre-aggregating tooltip values column-wise and extending the DataFrame in a more vectorized way instead of mutating individual cells.

Not blocking — just a performance consideration.

@souro26
Copy link
Copy Markdown
Contributor

souro26 commented Feb 28, 2026

The tooltip handling in draw_agents currently does nested loops with per-cell df.loc[i, key] = value writes. With n agents and k tooltip fields per agent, the algorithm is O(n·k). The main issue is the repeated scalar .loc writes, since those are expensive in pandas and add a large constant cost. That likely contributes to the Schelling regression in the benchmarks.

A cleaner approach would be to compute the full set of tooltip keys once, pre-allocate python lists of length n for each key., fill them in one O(n·k) pass and assign each column to the df once.

This avoids repeated df mutation and should reduce the overhead. Given the measurable runtime increase, I think this part is worth restructuring.

@souro26
Copy link
Copy Markdown
Contributor

souro26 commented Feb 28, 2026

There’s also an issue in how tooltip columns are determined.

Currently, the keys are taken from the first non-None tooltip only. If a later agent provides additional tooltip keys, those columns will still be created in the df via df.loc, but they won’t be included in tooltip_list. This can result in a mismatch between the df columns and the configured Altair tooltips.

A safer approach would be to compute the union of tooltip keys across all agents before constructing tooltip_list, for example by collecting all keys from non-None tooltips first and then extending tooltip_list with that full set.

@annapurna-gupta
Copy link
Copy Markdown
Contributor Author

Thanks @souro26 and @SAKSHI-DOT693 for the helpful feedback!

I'll fix:

  1. Collect ALL unique tooltip keys (not just the first).
  2. Pre-build columns vectorized instead of using df.loc in loops

This will fix both the performance regression and ensure all tooltip fields show up. Updating the PR now.

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Feb 28, 2026

Since the Schelling benchmarks show a slight runtime increase, I’m wondering if the row-wise DataFrame mutation might be contributing.

Just to clarify: the benchmarks only run the models without the visualization being involved. There is some variability simply due to using github actions.

Copy link
Copy Markdown
Contributor

@souro26 souro26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. Just left one small suggestion.

@annapurna-gupta annapurna-gupta requested a review from quaquel March 2, 2026 10:16
@quaquel
Copy link
Copy Markdown
Member

quaquel commented Mar 2, 2026

Thanks for this PR. I tested it locally, and it seems to be working with 1 caveat: why are x,y included? I guess this is some default behavior somewhere. However, with the explicit control this PR adds, I suggest we remove the coordinates so that only what the user specifies is included in the tooltip.

Copy link
Copy Markdown
Member

@quaquel quaquel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine but with one remaining minor request.

@annapurna-gupta
Copy link
Copy Markdown
Contributor Author

annapurna-gupta commented Mar 2, 2026

Thanks for this PR. I tested it locally, and it seems to be working with 1 caveat: why are x,y included? I guess this is some default behavior somewhere. However, with the explicit control this PR adds, I suggest we remove the coordinates so that only what the user specifies is included in the tooltip.

Thanks and I’ll remove the default x and y so only specified fields are included.

@annapurna-gupta annapurna-gupta requested a review from quaquel March 2, 2026 14:07
@quaquel quaquel merged commit c048cfc into mesa:main Mar 2, 2026
12 of 13 checks passed
@EwoutH EwoutH added the enhancement Release notes label label Mar 13, 2026
EwoutH pushed a commit that referenced this pull request Mar 15, 2026
* Add tooltip support to Altair backend

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* refactor: improve tooltip handling with vectorized ops and fix key collection as suggested

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Sort tooltip keys for consistent column order

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: issue warning for unsupported tooltip instead of raising exception

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove default x,y from tooltip

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
EwoutH pushed a commit that referenced this pull request Mar 15, 2026
* Add tooltip support to Altair backend

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* refactor: improve tooltip handling with vectorized ops and fix key collection as suggested

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Sort tooltip keys for consistent column order

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: issue warning for unsupported tooltip instead of raising exception

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove default x,y from tooltip

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Display info when clicking agents

5 participants