Skip to content

Commit af3e667

Browse files
sanika-nquaquel
andauthored
Added property layer viz to sugarscape (#2653)
* first commit * updated formatting --------- Co-authored-by: Jan Kwakkel <j.h.kwakkel@tudelft.nl>
1 parent 061de7a commit af3e667

File tree

1 file changed

+15
-38
lines changed
  • mesa/examples/advanced/sugarscape_g1mt

1 file changed

+15
-38
lines changed

mesa/examples/advanced/sugarscape_g1mt/app.py

+15-38
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,24 @@
1-
import numpy as np
2-
import solara
3-
from matplotlib.figure import Figure
4-
51
from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt
62
from mesa.visualization import Slider, SolaraViz, make_plot_component
3+
from mesa.visualization.components.matplotlib_components import make_mpl_space_component
4+
75

6+
def agent_portrayal(agent):
7+
return {"marker": "o", "color": "red", "size": 10}
88

9-
def SpaceDrawer(model):
10-
def portray(g):
11-
layers = {
12-
"trader": {"x": [], "y": [], "c": "tab:red", "marker": "o", "s": 10},
13-
}
149

15-
for agent in g.all_cells.agents:
16-
i, j = agent.cell.coordinate
17-
layers["trader"]["x"].append(i)
18-
layers["trader"]["y"].append(j)
19-
return layers
10+
propertylayer_portrayal = {
11+
"sugar": {"color": "blue", "alpha": 0.8, "colorbar": True, "vmin": 0, "vmax": 10},
12+
"spice": {"color": "red", "alpha": 0.8, "colorbar": True, "vmin": 0, "vmax": 10},
13+
}
2014

21-
fig = Figure()
22-
ax = fig.subplots()
23-
out = portray(model.grid)
24-
# Sugar
25-
# Important note: imshow by default draws from upper left. You have to
26-
# always explicitly specify origin="lower".
27-
im = ax.imshow(
28-
np.ma.masked_where(model.grid.sugar.data <= 1, model.grid.sugar.data),
29-
cmap="spring",
30-
origin="lower",
31-
)
32-
fig.colorbar(im, ax=ax, orientation="vertical", pad=0.1, fraction=0.046)
33-
# Spice
34-
im_spice = ax.imshow(
35-
np.ma.masked_where(model.grid.spice.data <= 1, model.grid.spice.data),
36-
cmap="winter",
37-
origin="lower",
38-
)
39-
fig.colorbar(im_spice, ax=ax, orientation="vertical", fraction=0.046, pad=0.04)
40-
# Trader
41-
ax.scatter(**out["trader"])
42-
ax.set_axis_off()
43-
return solara.FigureMatplotlib(fig)
4415

16+
sugarscape_space = make_mpl_space_component(
17+
agent_portrayal=agent_portrayal,
18+
propertylayer_portrayal=propertylayer_portrayal,
19+
post_process=None,
20+
draw_grid=False,
21+
)
4522

4623
model_params = {
4724
"seed": {
@@ -73,7 +50,7 @@ def portray(g):
7350
page = SolaraViz(
7451
model,
7552
components=[
76-
SpaceDrawer,
53+
sugarscape_space,
7754
make_plot_component("#Traders"),
7855
make_plot_component("Price"),
7956
],

0 commit comments

Comments
 (0)