Skip to content

Fix scalar color handling in legacy matplotlib backend.#2959

Merged
Sahil-Chhoker merged 5 commits intomesa:mainfrom
falloficarus22:fix-backend-scalar-color
Dec 16, 2025
Merged

Fix scalar color handling in legacy matplotlib backend.#2959
Sahil-Chhoker merged 5 commits intomesa:mainfrom
falloficarus22:fix-backend-scalar-color

Conversation

@falloficarus22
Copy link
Copy Markdown
Contributor

@falloficarus22 falloficarus22 commented Dec 16, 2025

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 AgentPortrayalStyle API and verify this fix.

Bug / Issue

Closes #2958
When an agent portrayal returned a numeric/scalar value for color (e.g., agent.wealth for a colormap), mpl_space_drawing.py attempted to assign this same scalar value to edgecolors as a default. This caused a ValueError in Matplotlib, which expects a valid color specification (string or RGB tuple) for edgecolors, 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

  • mesa/visualization/mpl_space_drawing.py: Modified the logic in collect_agent_data to check if aps.color is a number (int, float, etc.) before defaulting aps.edgecolors to it. If it is a number, edgecolors is left as None (or its separate default), preventing the crash.

Testing

  • Ran pytest tests/test_examples_viz.py locally.
  • Confirmed that test_boltzmann_wealth_model now passes without errors.
  • Verified that the fix allows the visualization to render correctly even when color is a scalar value mapped via a colormap.

Additional Notes

This fix was requested by reviewers in a previous PR #2934 to be separated out.

@falloficarus22
Copy link
Copy Markdown
Contributor Author

@Sahil-Chhoker review

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.8% [-2.1%, +0.4%] 🔵 +0.8% [+0.7%, +0.9%]
BoltzmannWealth large 🔵 +0.4% [-1.0%, +1.6%] 🔵 -0.2% [-3.1%, +2.5%]
Schelling small 🔵 -0.1% [-0.9%, +0.9%] 🔵 +0.4% [-0.4%, +1.2%]
Schelling large 🔵 +0.2% [-2.5%, +1.9%] 🔵 +1.3% [-1.6%, +4.1%]
WolfSheep small 🔵 -2.5% [-4.3%, -0.9%] 🔵 +0.2% [-0.1%, +0.5%]
WolfSheep large 🔵 +3.8% [+1.5%, +7.5%] 🔴 +7.4% [+4.7%, +10.4%]
BoidFlockers small 🔵 +0.4% [-0.1%, +0.9%] 🔵 -0.4% [-0.6%, -0.2%]
BoidFlockers large 🔵 -0.1% [-0.7%, +0.4%] 🔵 -0.9% [-1.1%, -0.7%]

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator

Sahil-Chhoker commented Dec 16, 2025

You were supposed to push only the fix, not the tests. This PR alone would have been enough, there was no need to create an issue. But what’s done is done, let’s leave it.
Also the github diff is not showing correctly:
image

Let's wait for the github to fix itself or do you have any idea what's causing this?

@falloficarus22
Copy link
Copy Markdown
Contributor Author

You were supposed to push only the fix, not the tests. This PR alone would have been enough, there was no need to create an issue. But what’s done is done, let’s leave it. Also the github diff is not showing correctly: image

Let's wait for the github to fix itself or do you have any idea what's causing this?

No idea, should I try committing again.

- 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.
@falloficarus22 falloficarus22 force-pushed the fix-backend-scalar-color branch from 96ca377 to 2172dd3 Compare December 16, 2025 09:52
@falloficarus22
Copy link
Copy Markdown
Contributor Author

@Sahil-Chhoker now it should look clean

@Sahil-Chhoker
Copy link
Copy Markdown
Collaborator

@falloficarus22 yeah, it's fixed now, can you remove the test.

@Sahil-Chhoker Sahil-Chhoker added the bug Release notes label label Dec 16, 2025
@Sahil-Chhoker Sahil-Chhoker changed the title Fix scalar color handling in backend & update Boltzmann viz test Fix scalar color handling in backend. Dec 16, 2025
@falloficarus22
Copy link
Copy Markdown
Contributor Author

@falloficarus22 yeah, it's fixed now, can you remove the test.

Okay

@Sahil-Chhoker Sahil-Chhoker changed the title Fix scalar color handling in backend. Fix scalar color handling in mpl_draw_matplotlib backend. Dec 16, 2025
@Sahil-Chhoker Sahil-Chhoker changed the title Fix scalar color handling in mpl_draw_matplotlib backend. Fix scalar color handling in legacy backend. Dec 16, 2025
@Sahil-Chhoker Sahil-Chhoker changed the title Fix scalar color handling in legacy backend. Fix scalar color handling in legacy matplotlib backend. Dec 16, 2025
falloficarus22 and others added 3 commits December 16, 2025 10:07
- 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.
@falloficarus22
Copy link
Copy Markdown
Contributor Author

@Sahil-Chhoker now it should be mergeable

Copy link
Copy Markdown
Collaborator

@Sahil-Chhoker Sahil-Chhoker left a comment

Choose a reason for hiding this comment

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

Good work!

@Sahil-Chhoker Sahil-Chhoker merged commit efaa9ba into mesa:main Dec 16, 2025
21 of 34 checks passed
@falloficarus22
Copy link
Copy Markdown
Contributor Author

Good work!

Thanks!

@falloficarus22 falloficarus22 deleted the fix-backend-scalar-color branch December 16, 2025 11:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Matplotlib Backend Crashes when Scalar/Numeric Values are used for Color

2 participants