Repository navigation
Fix z-order masking in matplotlib scatter - #3928
Merged
Merged
Conversation
|
Performance benchmarks:
|
quaquel
approved these changes
Oct 9, 2026
EwoutH
pushed a commit
that referenced
this pull request
Oct 28, 2026
(cherry picked from commit 6d55c55)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.