Add tooltip support to Altair backend#3392
Conversation
|
Performance benchmarks:
|
|
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. |
|
The tooltip handling in 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. |
|
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 A safer approach would be to compute the union of tooltip keys across all agents before constructing |
|
Thanks @souro26 and @SAKSHI-DOT693 for the helpful feedback! I'll fix:
This will fix both the performance regression and ensure all tooltip fields show up. Updating the PR now. |
Just to clarify: the benchmarks only run the models without the visualization being involved. There is some variability simply due to using github actions. |
souro26
left a comment
There was a problem hiding this comment.
Looks good overall. Just left one small suggestion.
|
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. |
quaquel
left a comment
There was a problem hiding this comment.
fine but with one remaining minor request.
Thanks and I’ll remove the default |
* 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>
* 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>
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
Added an optional tooltip field to AgentPortrayalStyle so users can pass extra info to show on hover.
Updated the Altair backend to collect this data, add it to the DataFrame, and display it using Altair tooltips.
Added a check in the Matplotlib backend to raise an error if tooltip is used (since Matplotlib is not interactive).
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}, )