Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions mesa/visualization/backends/matplotlib_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ def collect_agent_data(self, space, agent_portrayal, default_size=None):
else:
aps = portray_input
# Set defaults if not provided
if aps.edgecolors is None:
aps.edgecolors = aps.color
if aps.x is None and aps.y is None:
aps.x, aps.y = self._get_agent_pos(agent, space)

Expand Down
11 changes: 10 additions & 1 deletion mesa/visualization/components/portrayal_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from dataclasses import dataclass
from typing import Any

ColorLike = str | tuple | int | float


@dataclass
class AgentPortrayalStyle:
Expand Down Expand Up @@ -46,7 +48,7 @@ class AgentPortrayalStyle:

x: float | None = None
y: float | None = None
color: str | tuple | None = "tab:blue"
color: ColorLike | None = "tab:blue"
marker: str | None = "o"
size: int | float | None = 50
zorder: int | None = 1
Expand All @@ -72,6 +74,13 @@ def update(self, *updates_fields: tuple[str, Any]):
f"'{type(self).__name__}' object has no attribute '{field_to_change}'"
)

def __post_init__(self):
"""Validate that color and size are non-negative."""
if isinstance(self.color, int | float) and self.color < 0:
raise ValueError("Scalar color values must be non-negative")
if isinstance(self.size, int | float) and self.size < 0:
raise ValueError("Size must be a non-negative number")


@dataclass
class PropertyLayerStyle:
Expand Down
Loading