Fix scalar color handling in legacy matplotlib backend.#2959
Merged
Sahil-Chhoker merged 5 commits intomesa:mainfrom Dec 16, 2025
Merged
Fix scalar color handling in legacy matplotlib backend.#2959Sahil-Chhoker merged 5 commits intomesa:mainfrom
matplotlib backend.#2959Sahil-Chhoker merged 5 commits intomesa:mainfrom
Conversation
Contributor
Author
|
@Sahil-Chhoker review |
|
Performance benchmarks:
|
Collaborator
Contributor
Author
- Correctly handles scalar/numeric values (like int or float) for color in [mpl_space_drawing.py](cci:7://file://wsl.localhost/Ubuntu/root/mesa/mesa/visualization/mpl_space_drawing.py:0:0-0:0) by avoiding direct assignment to edgecolors, preventing crashes. - Updates [tests/test_examples_viz.py](cci:7://file://wsl.localhost/Ubuntu/root/mesa/tests/test_examples_viz.py:0:0-0:0) to use for the Boltzmann Wealth model test, ensuring consistent API usage and proper color mapping verification.
96ca377 to
2172dd3
Compare
Contributor
Author
|
@Sahil-Chhoker now it should look clean |
Collaborator
|
@falloficarus22 yeah, it's fixed now, can you remove the test. |
Contributor
Author
Okay |
mpl_draw_matplotlib backend.
mpl_draw_matplotlib backend.matplotlib backend.
- Correctly handles scalar/numeric values (like int or float) for color in [mpl_space_drawing.py](cci:7://file://wsl.localhost/Ubuntu/root/mesa/mesa/visualization/mpl_space_drawing.py:0:0-0:0) by avoiding direct assignment to edgecolors, preventing crashes. - Updates [tests/test_examples_viz.py](cci:7://file://wsl.localhost/Ubuntu/root/mesa/tests/test_examples_viz.py:0:0-0:0) to use for the Boltzmann Wealth model test, ensuring consistent API usage and proper color mapping verification.
- Correctly handles scalar/numeric values (like int or float) for color in [mpl_space_drawing.py](cci:7://file://wsl.localhost/Ubuntu/root/mesa/mesa/visualization/mpl_space_drawing.py:0:0-0:0) by avoiding direct assignment to edgecolors, preventing crashes. - Updates [tests/test_examples_viz.py](cci:7://file://wsl.localhost/Ubuntu/root/mesa/tests/test_examples_viz.py:0:0-0:0) to use for the Boltzmann Wealth model test, ensuring consistent API usage and proper color mapping verification.
Contributor
Author
|
@Sahil-Chhoker now it should be mergeable |
Contributor
Author
Thanks! |
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
This PR fixes a bug in the Matplotlib backend where using scalar/numeric values for agent colors caused a crash. It also updates the Boltzmann Wealth model test to properly utilize the new
AgentPortrayalStyleAPI and verify this fix.Bug / Issue
Closes #2958
When an agent portrayal returned a numeric/scalar value for color (e.g.,
agent.wealthfor a colormap), mpl_space_drawing.py attempted to assign this same scalar value toedgecolorsas a default. This caused aValueErrorin Matplotlib, which expects a valid color specification (string or RGB tuple) foredgecolors, not a scalar.Expected: The backend should handle scalar colors for colormaps gracefully and not blindly copy them to
edgecolors.Actual: The backend crashed with an invalid color format error.
Implementation
aps.coloris a number (int, float, etc.) before defaultingaps.edgecolorsto it. If it is a number,edgecolorsis left asNone(or its separate default), preventing the crash.Testing
pytest tests/test_examples_viz.pylocally.Additional Notes
This fix was requested by reviewers in a previous PR #2934 to be separated out.