Skip to content

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

@falloficarus22

Description

@falloficarus22

Description
I encountered a bug in mesa/visualization/mpl_space_drawing.py where passing scalar or numeric values (like int or float) for the color attribute in AgentPortrayalStyle causes a crash.

The issue arises because the backend attempts to default edgecolors to match the face color if edgecolors is not provided. However, when color is a scalar (intended for use with a colormap), directly assigning it to edgecolors (which expects a color specification, not a scalar mapping value) leads to a failure in Matplotlib.

Expected behavior
The backend should correctly handle scalar values for color (interpreting them via the colormap) and should validation or avoid assigning invalid scalar values to edgecolors automatically.

Proposed Fix
In collect_agent_data, add a check to ensure that edgecolors is only defaulted to color if color is a valid color specification and NOT a scalar/numeric value.

To Reproduce

  • Create a model where an agent's portrayal returns an AgentPortrayalStyle with a scalar value for color (e.g.,color=agent.wealth).
  • Run the visualization using the Matplotlib backend.
  • Observe the crash/error.

Additional Notes
This issue was discovered while fixing test suite warnings in #2904 with a fix PR #2934

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelease notes label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions