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
83 changes: 60 additions & 23 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,44 @@
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": []})

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

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

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L33-L37

Added lines #L33 - L37 were not covered by tests

if not (len(x) == len(y) == len(s) == len(c) == len(m)):
raise ValueError(

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

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L40

Added line #L40 was not covered by tests
"Length mismatch in portrayal data lists: "
rmhopkins4 marked this conversation as resolved.
Show resolved Hide resolved
f"x: {len(x)}, y: {len(y)}, s: {len(s)}, "
f"c: {len(c)}, m: {len(m)}"
)

# Group the data by marker
for i in range(len(x)):
marker = m[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 52 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L48-L52

Added lines #L48 - L52 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 56 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L56

Added line #L56 was not covered by tests


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

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

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L65

Added line #L65 was not covered by tests
for i in range(g.width):
for j in range(g.height):
content = g._grid[i][j]
Expand All @@ -41,23 +75,23 @@
data = agent_portrayal(agent)
x.append(i)
y.append(j)
if "size" in data:
s.append(data["size"])
if "color" in data:
c.append(data["color"])
out = {"x": x, "y": y}
# This is the default value for the marker size, which auto-scales
# according to the grid area.
out["s"] = (180 / max(g.width, g.height)) ** 2
if len(s) > 0:
out["s"] = s
if len(c) > 0:
out["c"] = c

# This is the default value for the marker size, which auto-scales
# according to the grid area.
default_size = (180 / max(g.width, g.height)) ** 2

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
# establishing a default prevents misalignment if some agents are not given size, color, etc.
size = data.get("size", default_size)
s.append(size)
color = data.get("color", "b")
rmhopkins4 marked this conversation as resolved.
Show resolved Hide resolved
c.append(color)
mark = data.get("shape", ".")
rmhopkins4 marked this conversation as resolved.
Show resolved Hide resolved
m.append(mark)
out = {"x": x, "y": y, "s": s, "c": c, "m": m}

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

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L83-L89

Added lines #L83 - L89 were 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 94 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L94

Added line #L94 was not covered by tests


def _draw_network_grid(space, space_ax, agent_portrayal):
Expand All @@ -77,20 +111,23 @@
y = []
s = [] # size
c = [] # color
m = [] # shape

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

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L114

Added line #L114 was not covered by tests
for agent in space._agent_to_index:
data = agent_portrayal(agent)
_x, _y = agent.pos
x.append(_x)
y.append(_y)
if "size" in data:
s.append(data["size"])
if "color" in data:
c.append(data["color"])
out = {"x": x, "y": y}
if len(s) > 0:
out["s"] = s
if len(c) > 0:
out["c"] = c

# This is matplotlib's default marker size
default_size = 20

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

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L122

Added line #L122 was not covered by tests
# establishing a default prevents misalignment if some agents are not given size, color, etc.
size = data.get("size", default_size)
s.append(size)
color = data.get("color", "b")
c.append(color)
mark = data.get("shape", ".")
m.append(mark)
out = {"x": x, "y": y, "s": s, "c": c, "m": m}

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

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L124-L130

Added lines #L124 - L130 were not covered by tests
return out

# Determine border style based on space.torus
Expand All @@ -110,7 +147,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 150 in mesa/visualization/components/matplotlib.py

View check run for this annotation

Codecov / codecov/patch

mesa/visualization/components/matplotlib.py#L150

Added line #L150 was not covered by tests


@solara.component
Expand Down