Skip to content

Draw HexGrid agents at the centre of their hexagon - #3952

Merged
quaquel merged 2 commits into
mesa:mainfrom
AshNicolus:fix/hexgrid-agent-positions
Nov 10, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
AshNicolus:fix/hexgrid-agent-positions

Conversation

@AshNicolus

@AshNicolus AshNicolus commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

With SpaceRenderer, agents on a HexGrid are not drawn inside their own hexagon. They get pushed up and to the right, more so the further they are from the origin, and some end up outside the plot. This PR puts every agent back at the centre of its hexagon.

Bug / Issue

Fixes #3949

Since #3268, HexGrid stores the real (pixel) centre of each hexagon in cell.position. The hexagons are drawn from that, and agent positions come from it too. But _map_coordinates still thought it was getting grid coordinates, so it converted them a second time:

flowchart LR
    A["cell (2, 1)"] --> B["cell.position<br/>already the hexagon centre"]
    B --> C["_map_coordinates<br/>applies hex spacing again"]
    C --> D[agent drawn in the wrong place]
    B --> E[hexagon drawn here]
Loading

Implementation

HexGrid now goes through the same branch as the other spaces in _map_coordinates, so its positions are used as they are. The old hex conversion is removed.

For example, an agent in cell (0, 1):

x y
hexagon centre (cell.position) 0.866 1.5
agent drawn at, before 1.933 2.25
agent drawn at, after 0.866 1.5

Testing

  • The old hex check in test_map_coordinates only asserted that the output was different from the input, which is how this slipped through. It now expects the positions to be unchanged.
  • Added test_hexgrid_agents_drawn_at_hexagon_centre for both backends. It places agents on even and odd rows and checks each one lands on the centre of the hexagon the drawer draws for its cell. It fails on main and passes with this change.
  • Full suite passes locally with pytest tests/ -Werror (877 passed), and pre-commit passes.
  • Rendered a 6x6 grid with 6 agents before and after. Before, only 4 of the 6 were visible and none sat in its hexagon. After, all 6 sit in the middle of their hexagons.

Additional Notes

The legacy draw_space path in mpl_space_drawing.py has the same double conversion, and its hexagon mesh also shifts even rows while HexGrid shifts odd rows. Fixing that means changing how the legacy mesh is drawn, so I kept it out of this PR to keep it small. Happy to open a follow-up if that's useful.

Since mesa#3268, HexGrid stores the pixel centre of each hexagon in
cell.position. The drawer already uses it for the hexagons, but
SpaceRenderer._map_coordinates still treated the agent positions as
grid coordinates and converted them again. Agents ended up shifted
away from their cell, and some were drawn outside the plot.

Use the positions as they are, like the other space types. Replace the
old test, which only checked that the positions changed, with one that
checks each agent is drawn on its own hexagon for both backends.

Fixes mesa#3949
@github-actions github-actions Bot added testing Release notes label visualization labels Oct 19, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.6% [-2.3%, -1.1%] 🔵 -0.7% [-1.0%, -0.5%]
BoltzmannWealth large 🔵 +0.4% [-0.6%, +1.4%] 🔵 -0.2% [-4.1%, +3.6%]
Schelling small 🔵 +1.0% [+0.3%, +1.7%] 🔵 -0.9% [-1.8%, -0.1%]
Schelling large 🔵 +1.5% [+1.1%, +2.0%] 🔴 +4.7% [+3.0%, +6.5%]
WolfSheep small 🔵 -0.4% [-0.8%, +0.0%] 🔵 -1.3% [-5.0%, +2.3%]
WolfSheep large 🔵 +0.5% [-0.7%, +1.6%] 🔵 -1.7% [-3.5%, +0.2%]
SugarscapeG1mt small 🔴 +3.5% [+3.0%, +4.0%] 🔵 +2.6% [+0.8%, +4.3%]
SugarscapeG1mt large 🔵 +1.5% [-0.1%, +3.4%] 🔵 +0.8% [-1.8%, +3.3%]
BoidFlockers small 🔵 -1.0% [-1.3%, -0.6%] 🔵 -3.0% [-3.3%, -2.7%]
BoidFlockers large 🔵 -0.7% [-1.0%, -0.2%] 🔵 -2.0% [-2.3%, -1.7%]
MultiLevelAllianceModel small 🔵 -1.8% [-3.2%, -0.3%] 🔵 +0.2% [-0.1%, +0.6%]
MultiLevelAllianceModel large 🔵 -0.7% [-1.5%, +0.1%] 🔵 +2.2% [+0.4%, +4.1%]

@mesa-triage-bot

Copy link
Copy Markdown

Related work: #4043 now covers the legacy rendering issue noted in the description, and #3959 addresses the HexGrid row layout. Using cell.position directly here should keep agent markers aligned with their hexagons under either row layout.


Automated triage note, written by an AI model for the Mesa maintainers. It can be wrong, and it doesn't make decisions: maintainers do. If it missed the point, say so here.

@quaquel

quaquel commented Nov 10, 2026

Copy link
Copy Markdown
Member

Could you add a before and after screenshot? Otherwise this looks fine.

@AshNicolus

Copy link
Copy Markdown
Contributor Author

Added a before/after: a 6×6 HexGrid with one agent per cell, drawn with SpaceRenderer (matplotlib).

Before After
Before After

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

@quaquel
quaquel merged commit e988375 into mesa:main Nov 10, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Release notes label visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SpaceRenderer draws HexGrid agents off their hexagon (hex spacing applied twice)

2 participants