Skip to content

fix(viz): align HexGrid property layer values with hexagon mesh - #4046

Merged
EwoutH merged 2 commits into
mesa:mainfrom
pojeesahil:fix-hexgrid-property-layer-alignment
Nov 16, 2026
Merged

EwoutH merged 2 commits into
mesa:mainfrom
pojeesahil:fix-hexgrid-property-layer-alignment

Conversation

@pojeesahil

@pojeesahil pojeesahil commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

Fixes property layer visualization on HexGrid where colors are drawn on the wrong hexagons due to mismatched array ordering.

Bug / Issue

When visualizing a property layer on a HexGrid using Matplotlib, the cell values show up on the wrong hexagons.

For example, on a 4x2 hex grid, setting a value at coordinate (3, 0) does not draw color at (3, 0). Instead, it shows up at (1, 1).

Why this happens:
The list of hexagon shapes and the data array values are ordered differently:

  • In SpaceRenderer (matplotlib_backend.py), the hexagons follow the model's cells in (x, y) order. However, the code flattened transposed data (data.T.ravel()), which swapped the axes and placed colors on the wrong cells.
  • In legacy draw_property_layers (mpl_space_drawing.py), the reverse happened. The hexagon shapes were created row-by-row, but the data array was flattened without being transposed.

Implementation

  • mesa/visualization/backends/matplotlib_backend.py: Changed data.T.ravel() to data.ravel() so the flattened values match the cell order of space_drawer.hexagons.
  • mesa/visualization/mpl_space_drawing.py: Changed data.ravel() to data.T.ravel() so the flattened values match the row-major mesh generated by _get_hexmesh.
  • Added regression tests in both test_space_renderer.py and test_components_matplotlib.py verifying that a value placed at (3, 0) on a 4x2 HexGrid correctly colors hexagon (3, 0).

Testing

  • Ran pytest tests/visualization/ (all 129 tests passed).
  • Verified with ruff check and ruff format.
image

@github-actions github-actions Bot added testing Release notes label visualization labels Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.1% [-1.5%, -0.7%] 🔵 +0.7% [+0.5%, +1.0%]
BoltzmannWealth large 🔵 -0.8% [-1.2%, -0.5%] 🔵 -0.1% [-0.3%, +0.0%]
Schelling small 🔵 +1.4% [+0.9%, +2.0%] 🔵 +1.2% [+0.4%, +2.1%]
Schelling large 🔵 -1.2% [-2.0%, -0.4%] 🔵 +0.1% [-0.9%, +0.7%]
WolfSheep small 🔵 +1.1% [+0.8%, +1.5%] 🔵 +0.8% [-3.9%, +5.3%]
WolfSheep large 🔵 -0.5% [-1.2%, +0.2%] 🔵 +0.3% [-0.6%, +1.3%]
SugarscapeG1mt small 🔵 +1.2% [+0.7%, +1.6%] 🔵 -0.2% [-1.8%, +1.4%]
SugarscapeG1mt large 🔵 -1.1% [-2.2%, -0.1%] 🔵 -1.5% [-3.6%, +0.6%]
BoidFlockers small 🔵 -0.7% [-0.9%, -0.5%] 🔵 -0.8% [-1.2%, -0.5%]
BoidFlockers large 🔵 -0.4% [-0.6%, -0.2%] 🔵 -0.2% [-0.5%, +0.1%]
MultiLevelAllianceModel small 🔵 +1.5% [+0.7%, +2.1%] 🔵 -0.0% [-0.4%, +0.4%]
MultiLevelAllianceModel large 🔵 +1.5% [+0.9%, +2.0%] 🔵 +0.0% [-0.3%, +0.3%]

@AshNicolus AshNicolus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checked this against main and with my #4043 and #3952 stacked on top. On a 5x3 HexGrid with three different values and an agent on each of those cells, the coloured hexagons and the agents line up in both the legacy draw_space path and SpaceRenderer; without this PR they don't, in either path. I also confirmed the reasoning in grid.py: cells come from product(range(width), range(height)), so all_cells is x-major, while _get_hexmesh is row-major. Both new tests fail on main and pass here, the full suite passes with all three PRs stacked, and #4043 merges cleanly with this one.

flowchart LR
    subgraph New["SpaceRenderer (matplotlib backend)"]
        N1["hexagons from space.all_cells<br/>x-major: (0,0),(0,1),(1,0)..."] --> N3{"same order?"}
        N2["data shape (width, height)"] -->|"main: data.T.ravel() → row-major"| N3
        N2 -->|"PR: data.ravel() → x-major"| N3
    end
    subgraph Legacy["legacy draw_property_layers"]
        L1["_get_hexmesh<br/>row-major: (0,0),(1,0),(2,0)..."] --> L3{"same order?"}
        L2["data shape (width, height)"] -->|"main: data.ravel() → x-major"| L3
        L2 -->|"PR: data.T.ravel() → row-major"| L3
    end
Loading

Two small optional suggestions inline: a one-line comment at each ravel saying which order it has to match, since that implicit ordering is what let this slip in. LGTM.

Comment thread mesa/visualization/mpl_space_drawing.py Resolved
@pojeesahil

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the helpful diagrams, I have added both the suggested comments.

@pojeesahil
pojeesahil force-pushed the fix-hexgrid-property-layer-alignment branch from 614fe4a to 6966733 Compare November 13, 2026 13:20
@mesa-triage-bot

Copy link
Copy Markdown

Thanks for fixing the HexGrid property-layer alignment, @pojeesahil. The reported checks pass, but the branch now has merge conflicts with main.

Please rebase onto main, resolve the conflicts, and rerun the visualization tests so this can proceed to maintainer review.

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

@pojeesahil
pojeesahil force-pushed the fix-hexgrid-property-layer-alignment branch from 6966733 to 6ed9dae Compare November 15, 2026 07:40
@pojeesahil

Copy link
Copy Markdown
Contributor Author

Please rebase onto main, resolve the conflicts, and rerun the visualization tests so this can proceed to maintainer review.

Done

@EwoutH EwoutH left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this must have been a very fustrating bug for users to encounter. Difficult to debug if the visualisation is lying to you!

pojeesahil and others added 2 commits November 16, 2026 23:20
@EwoutH
EwoutH force-pushed the fix-hexgrid-property-layer-alignment branch from 6ed9dae to 8de2f7f Compare November 16, 2026 22:20
@EwoutH
EwoutH merged commit ac849e1 into mesa:main Nov 16, 2026
14 of 15 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
Fixes property layer visualization on HexGrid where colors are drawn on the wrong hexagons due to mismatched array ordering.

Partly backported for 3.5.x: only the legacy draw_property_layers fix. On 3.5 the
SpaceRenderer hexagon mesh is still row-major, so the matplotlib backend already
uses data.T.ravel() correctly and is left unchanged. The SpaceRenderer test
checks the row-major order instead.

(cherry picked from commit ac849e1)

Assisted-by: Claude Opus 5.5
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.

3 participants