Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualisation: Allow specifying Agent shapes in agent_portrayal #2214

Merged
merged 8 commits into from
Aug 21, 2024
45 changes: 43 additions & 2 deletions mesa/visualization/components/matplotlib.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import defaultdict

import networkx as nx
import solara
from matplotlib.figure import Figure
Expand All @@ -23,12 +25,42 @@
solara.FigureMatplotlib(space_fig, format="png", dependencies=dependencies)


# matplotlib scatter does not allow for multiple shapes in one call
EwoutH marked this conversation as resolved.
Show resolved Hide resolved
def _split_and_scatter(portray_data, space_ax):
grouped_data = defaultdict(lambda: {"x": [], "y": [], "s": [], "c": []})

if "marker" not in portray_data:
# standard scatter is fine, default marker
space_ax.scatter(**portray_data)
return

Check warning on line 35 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L34-L35

Added lines #L34 - L35 were not covered by tests

# Extract data from the dictionary
markers = portray_data["marker"]
x = portray_data["x"]
y = portray_data["y"]
s = portray_data["s"]
c = portray_data["c"]

Check warning on line 42 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L38-L42

Added lines #L38 - L42 were not covered by tests

# Group the data by marker
for i in range(len(x)):
marker = markers[i]
grouped_data[marker]["x"].append(x[i])
grouped_data[marker]["y"].append(y[i])
grouped_data[marker]["s"].append(s[i])
grouped_data[marker]["c"].append(c[i])

Check warning on line 50 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L46-L50

Added lines #L46 - L50 were not covered by tests

# Plot each group with the same marker
for marker, data in grouped_data.items():
EwoutH marked this conversation as resolved.
Show resolved Hide resolved
space_ax.scatter(data["x"], data["y"], s=data["s"], c=data["c"], marker=marker)

Check warning on line 54 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L54

Added line #L54 was not covered by tests


def _draw_grid(space, space_ax, agent_portrayal):
def portray(g):
x = []
y = []
s = [] # size
c = [] # color
marker = [] # shape

Check warning on line 63 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L63

Added line #L63 was not covered by tests
for i in range(g.width):
for j in range(g.height):
content = g._grid[i][j]
Expand All @@ -45,6 +77,8 @@
s.append(data["size"])
if "color" in data:
c.append(data["color"])
if "shape" in data:
marker.append(data["shape"])

Check warning on line 81 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L81

Added line #L81 was not covered by tests
out = {"x": x, "y": y}
# This is the default value for the marker size, which auto-scales
# according to the grid area.
Expand All @@ -53,11 +87,13 @@
out["s"] = s
if len(c) > 0:
out["c"] = c
if len(marker) > 0:
out["marker"] = marker

Check warning on line 91 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L91

Added line #L91 was not covered by tests
return out

space_ax.set_xlim(-1, space.width)
space_ax.set_ylim(-1, space.height)
space_ax.scatter(**portray(space))
_split_and_scatter(portray(space), space_ax)

Check warning on line 96 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L96

Added line #L96 was not covered by tests


def _draw_network_grid(space, space_ax, agent_portrayal):
Expand All @@ -77,6 +113,7 @@
y = []
s = [] # size
c = [] # color
marker = [] # shape

Check warning on line 116 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L116

Added line #L116 was not covered by tests
for agent in space._agent_to_index:
data = agent_portrayal(agent)
_x, _y = agent.pos
Expand All @@ -86,11 +123,15 @@
s.append(data["size"])
if "color" in data:
c.append(data["color"])
if "shape" in data:
marker.append(data["shape"])

Check warning on line 127 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L127

Added line #L127 was not covered by tests
out = {"x": x, "y": y}
if len(s) > 0:
out["s"] = s
if len(c) > 0:
out["c"] = c
if len(marker) > 0:
out["marker"] = marker

Check warning on line 134 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L134

Added line #L134 was not covered by tests
return out

# Determine border style based on space.torus
Expand All @@ -110,7 +151,7 @@
space_ax.set_ylim(space.y_min - y_padding, space.y_max + y_padding)

# Portray and scatter the agents in the space
space_ax.scatter(**portray(space))
_split_and_scatter(portray(space), space_ax)

Check warning on line 154 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L154

Added line #L154 was not covered by tests


@solara.component
Expand Down