Skip to content

Fix z-order masking in matplotlib scatter - #3928

Merged
quaquel merged 1 commit into
mesa:mainfrom
CraftierTen5:bugfix/mpl-scatter-zorder
Oct 9, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
CraftierTen5:bugfix/mpl-scatter-zorder

Conversation

@CraftierTen5

Copy link
Copy Markdown
Contributor

Summary

Fixes an operator-precedence bug in the Matplotlib scatter rendering logic that causes agents with certain z-orders to be omitted from the visualization.

Bug / Issue

In _scatter() in mesa/visualization/mpl_space_drawing.py, the z-order mask was constructed as:

zorder_mask = z_order == zorder & mask_marker

Due to Python's operator precedence, this does not evaluate as (z_order == zorder) & mask_marker. As a result, agents with certain z-orders can receive an empty mask and fail to render. The expected behavior is that agents sharing the same marker are separated and rendered according to their individual z-orders.

A regression case with three agents using the same marker and z-orders 1, 2, and 3 reproduced the issue: the agents with higher z-orders were not rendered.

Implementation

Added parentheses around the equality comparison:
zorder_mask = (z_order == zorder) & mask_marker

Also added a regression test in tests/visualization/test_components_matplotlib.py that creates three agents with the same marker but different z-orders and verifies that each z-order produces a rendered collection containing one agent.

Testing

Regression test and existing tests in tests/visualization/test_components_matplotlib.py: 17 passed
Entire tests/visualization test suite: all tests passed
git diff --check: passed
Ruff checks: passed

Additional Notes

The change is limited to the z-order masking expression and its regression test. No new dependencies or API changes are introduced.

@github-actions github-actions Bot added testing Release notes label visualization labels Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.9% [+0.4%, +1.5%] 🔵 -0.0% [-0.2%, +0.1%]
BoltzmannWealth large 🔵 +1.2% [+0.9%, +1.6%] 🔴 +7.3% [+5.7%, +8.7%]
Schelling small 🔵 -0.2% [-0.7%, +0.3%] 🔵 +0.4% [-0.4%, +1.2%]
Schelling large 🔵 +0.2% [-0.3%, +0.7%] 🔵 +0.0% [-1.9%, +1.9%]
WolfSheep small 🔵 +0.5% [-0.1%, +1.1%] 🔵 +1.7% [-2.5%, +5.9%]
WolfSheep large 🔵 -1.0% [-1.5%, -0.4%] 🔵 -1.3% [-2.9%, +0.2%]
SugarscapeG1mt small 🔵 -0.8% [-1.5%, +0.1%] 🔵 -1.1% [-2.6%, +0.4%]
SugarscapeG1mt large 🔵 -0.8% [-3.7%, +2.3%] 🔵 -1.1% [-2.7%, +0.6%]
BoidFlockers small 🔵 -1.1% [-2.0%, -0.3%] 🔵 -1.4% [-1.9%, -0.8%]
BoidFlockers large 🔵 -1.8% [-2.9%, -0.6%] 🔵 -1.0% [-1.3%, -0.6%]
MultiLevelAllianceModel small 🔵 -0.5% [-2.4%, +1.4%] 🔵 -0.7% [-1.3%, -0.2%]
MultiLevelAllianceModel large 🔵 +0.7% [-1.8%, +3.0%] 🔵 -0.2% [-0.6%, +0.2%]

@quaquel quaquel added the bug Release notes label label Oct 9, 2026
@quaquel
quaquel merged commit 6d55c55 into mesa:main Oct 9, 2026
16 of 17 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants