Skip to content

Draw HexGrid agents at their hexagon in legacy draw_hex_grid - #4043

Closed
AshNicolus wants to merge 4 commits into
mesa:mainfrom
AshNicolus:fix/legacy-hexgrid-agent-positions
Closed

AshNicolus wants to merge 4 commits into
mesa:mainfrom
AshNicolus:fix/legacy-hexgrid-agent-positions

Conversation

@AshNicolus

@AshNicolus AshNicolus commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

The legacy draw_space / draw_hex_grid drew HexGrid agents away from their hexagons, many of them off the plot. Agents are now drawn at the centre of their own hexagon.

Bug / Issue

Fixes #4042.

collect_agent_data gives agent.cell.position, which since #3268 is already in pixels. draw_hex_grid then applied hex spacing to it a second time, the same double conversion #3952 fixes for SpaceRenderer.

flowchart LR
    P["cell.position<br/>already the hexagon centre (even-r since #3959)"] -->|main: spacing applied again| X["agent off its hexagon"]
    P -->|this PR: used as it is| H["agent at its hexagon centre"]
    M["_get_hexmesh<br/>even rows shifted"] --> H
Loading

On a 6x6 grid:

Cell Hexagon drawn at Agent, main Agent, this PR
(0, 1) (0, 1.5) (1.93, 2.25) (0, 1.5)
(2, 2) (4.33, 3) (6.0, 4.5) (4.33, 3)
(3, 3) (5.20, 4.5) (11.8, 6.75), off the plot (5.20, 4.5)
(5, 5) (8.66, 7.5) (16.9, 11.25), off the plot (8.66, 7.5)

Implementation

draw_hex_grid no longer converts the positions it gets from collect_agent_data. Since #3959, cell.position uses the same even-r layout as _get_hexmesh, so it is already the centre of the drawn hexagon. The conversion is removed, as @quaquel suggested.

Testing

  • Added test_draw_hex_grid_agents_at_hexagon_centre, which compares each drawn agent with the centre of its cell's hexagon from _get_hexmesh. It fails on main and passes here.
  • Re-rendered the 6x6 grid after the change: same result as the "after" screenshot below.
  • Full suite passes with pytest tests/ -Werror -Wdefault::PendingDeprecationWarning (1127 passed). Pre-commit passes.

Additional Notes

Only the legacy matplotlib path is touched. SpaceRenderer is handled by #3952.

draw_hex_grid applied hex spacing to agent.cell.position, which is already
in pixels, so agents drifted away from their hexagons and many left the plot.
Start from cell.coordinate instead and keep the existing even-r conversion,
which matches the hexagons from _get_hexmesh.

Fixes mesa#4042
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.1% [-0.4%, +0.5%] 🔵 +0.7% [+0.5%, +0.9%]
BoltzmannWealth large 🔵 +0.9% [-0.3%, +2.2%] 🔵 -1.1% [-2.0%, +0.0%]
Schelling small 🔵 +0.1% [-0.4%, +0.6%] 🔵 -1.3% [-2.2%, -0.5%]
Schelling large 🔵 -0.1% [-1.0%, +0.8%] 🔵 -2.9% [-6.2%, -0.4%]
WolfSheep small 🔵 -1.2% [-1.7%, -0.7%] 🔵 +1.9% [-2.3%, +6.4%]
WolfSheep large 🔵 -0.7% [-1.8%, +0.5%] 🔵 -1.8% [-3.0%, -0.6%]
SugarscapeG1mt small 🔵 -0.5% [-1.0%, +0.1%] 🔵 -0.3% [-2.2%, +1.4%]
SugarscapeG1mt large 🔵 -0.4% [-1.4%, +0.6%] 🔵 -0.5% [-1.7%, +0.8%]
BoidFlockers small 🔵 -1.9% [-2.4%, -1.2%] 🔵 +0.1% [-0.4%, +0.7%]
BoidFlockers large 🔵 -2.7% [-3.2%, -2.1%] 🔵 +0.2% [-0.1%, +0.5%]
MultiLevelAllianceModel small 🔵 -0.4% [-1.4%, +0.6%] 🔵 -0.1% [-0.4%, +0.1%]
MultiLevelAllianceModel large 🔵 -1.6% [-2.1%, -1.0%] 🔵 -0.1% [-0.4%, +0.1%]

@quaquel quaquel added the backport-candidate PRs we might want to backport to an earlier branch label Nov 14, 2026
@quaquel

quaquel commented Nov 14, 2026

Copy link
Copy Markdown
Member

Can you add a before after? Once I have seen that, I will go ahead and merge this.

@AshNicolus

Copy link
Copy Markdown
Contributor Author

Added a before/after: a 6×6 HexGrid with one agent per cell, drawn with the legacy draw_space.

Before After
hex_legacy_before hex_legacy_after

Before, agents land between hexagons and some fall outside the grid. After, each one sits at the centre of its own hexagon.

Comment thread mesa/visualization/mpl_space_drawing.py Resolved
@mesa-triage-bot

Copy link
Copy Markdown

Thanks @AshNicolus for the before/after screenshots and the row-layout explanation.

@quaquel, should this land with the coordinate conversion, or should #3959 land first so the author can use agent.cell.position directly? I'd prefer #3959 first: it aligns positions with the existing mesh and avoids maintaining another conversion.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

@AshNicolus

Copy link
Copy Markdown
Contributor Author

@EwoutH saw the note in #4130. i checked whether this is still needed on 3.5.x: on 3.5.x-maintenance (3.5.2) the legacy draw_hex_grid already puts all 16 agents of a 4x4 HexGrid on their hexagon centres, since 3.5 reads cell.coordinate there, not cell.position. #4129 doesn't change that, its only change to that file is the #4046 property layer fix. so there's nothing to backport, and the backport-candidate label can go. i'll close this once #4130 is merged, unless you'd rather merge it into main before then.

@mesa-triage-bot

Copy link
Copy Markdown

Thanks @AshNicolus for checking whether 3.5.x needs this fix.

@EwoutH, should this still merge before #4130, or wait for the legacy API removal? I recommend waiting for removal: the author reports that 3.5.x already draws these agents correctly, so there is no backport benefit. Please confirm which path to take.

Automated note by an AI model; it can be wrong, and maintainers make the decisions. About this bot.

@EwoutH

EwoutH commented Nov 18, 2026

Copy link
Copy Markdown
Member

I would I just merged #4130, so I think this can be closed.

@AshNicolus

Copy link
Copy Markdown
Contributor Author

closing, since #4130 removed the legacy drawing API from main, and 3.5.x already draws hex agents on their hexagons. thanks for the reviews!

@AshNicolus AshNicolus closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-candidate PRs we might want to backport to an earlier branch testing Release notes label visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Legacy draw_space draws HexGrid agents off their hexagons (hex spacing applied twice)

3 participants