forked from projectmesa/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move Matplotlib-specific Solara components to separate file
- Loading branch information
Showing
3 changed files
with
121 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
from typing import Optional | ||
|
||
import networkx as nx | ||
import solara | ||
from matplotlib.figure import Figure | ||
from matplotlib.ticker import MaxNLocator | ||
|
||
import mesa | ||
|
||
|
||
@solara.component | ||
def SpaceMatplotlib(model, agent_portrayal, dependencies: Optional[list[any]] = None): | ||
space_fig = Figure() | ||
space_ax = space_fig.subplots() | ||
space = getattr(model, "grid", None) | ||
if space is None: | ||
# Sometimes the space is defined as model.space instead of model.grid | ||
space = model.space | ||
if isinstance(space, mesa.space.NetworkGrid): | ||
_draw_network_grid(space, space_ax, agent_portrayal) | ||
elif isinstance(space, mesa.space.ContinuousSpace): | ||
_draw_continuous_space(space, space_ax, agent_portrayal) | ||
else: | ||
_draw_grid(space, space_ax, agent_portrayal) | ||
space_ax.set_axis_off() | ||
solara.FigureMatplotlib(space_fig, format="png", dependencies=dependencies) | ||
|
||
|
||
def _draw_grid(space, space_ax, agent_portrayal): | ||
def portray(g): | ||
x = [] | ||
y = [] | ||
s = [] # size | ||
c = [] # color | ||
for i in range(g.width): | ||
for j in range(g.height): | ||
content = g._grid[i][j] | ||
if not content: | ||
continue | ||
if not hasattr(content, "__iter__"): | ||
# Is a single grid | ||
content = [content] | ||
for agent in content: | ||
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} | ||
if len(s) > 0: | ||
out["s"] = s | ||
if len(c) > 0: | ||
out["c"] = c | ||
return out | ||
|
||
space_ax.scatter(**portray(space)) | ||
|
||
|
||
def _draw_network_grid(space, space_ax, agent_portrayal): | ||
graph = space.G | ||
pos = nx.spring_layout(graph, seed=0) | ||
nx.draw( | ||
graph, | ||
ax=space_ax, | ||
pos=pos, | ||
**agent_portrayal(graph), | ||
) | ||
|
||
|
||
def _draw_continuous_space(space, space_ax, agent_portrayal): | ||
def portray(space): | ||
x = [] | ||
y = [] | ||
s = [] # size | ||
c = [] # color | ||
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 | ||
return out | ||
|
||
space_ax.scatter(**portray(space)) | ||
|
||
|
||
def make_plot(model, measure): | ||
fig = Figure() | ||
ax = fig.subplots() | ||
df = model.datacollector.get_model_vars_dataframe() | ||
if isinstance(measure, str): | ||
ax.plot(df.loc[:, measure]) | ||
ax.set_ylabel(measure) | ||
elif isinstance(measure, dict): | ||
for m, color in measure.items(): | ||
ax.plot(df.loc[:, m], label=m, color=color) | ||
fig.legend() | ||
elif isinstance(measure, (list, tuple)): | ||
for m in measure: | ||
ax.plot(df.loc[:, m], label=m) | ||
fig.legend() | ||
# Set integer x axis | ||
ax.xaxis.set_major_locator(MaxNLocator(integer=True)) | ||
solara.FigureMatplotlib(fig) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters