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

Added property layer viz to sugarscape #2653

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Changes from 2 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
59 changes: 21 additions & 38 deletions mesa/examples/advanced/sugarscape_g1mt/app.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,31 @@
import numpy as np
import solara
from matplotlib.figure import Figure

from mesa.examples.advanced.sugarscape_g1mt.model import SugarscapeG1mt
from mesa.visualization import Slider, SolaraViz, make_plot_component
from mesa.visualization.components.matplotlib_components import make_mpl_space_component


def SpaceDrawer(model):
def portray(g):
layers = {
"trader": {"x": [], "y": [], "c": "tab:red", "marker": "o", "s": 10},
}
def agent_portrayal(agent):
return {"marker": "o", "color": "red", "size": 10}

Check warning on line 7 in mesa/examples/advanced/sugarscape_g1mt/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/advanced/sugarscape_g1mt/app.py#L7

Added line #L7 was not covered by tests

for agent in g.all_cells.agents:
i, j = agent.cell.coordinate
layers["trader"]["x"].append(i)
layers["trader"]["y"].append(j)
return layers

fig = Figure()
ax = fig.subplots()
out = portray(model.grid)
# Sugar
# Important note: imshow by default draws from upper left. You have to
# always explicitly specify origin="lower".
im = ax.imshow(
np.ma.masked_where(model.grid.sugar.data <= 1, model.grid.sugar.data),
cmap="spring",
origin="lower",
)
fig.colorbar(im, ax=ax, orientation="vertical", pad=0.1, fraction=0.046)
# Spice
im_spice = ax.imshow(
np.ma.masked_where(model.grid.spice.data <= 1, model.grid.spice.data),
cmap="winter",
origin="lower",
)
fig.colorbar(im_spice, ax=ax, orientation="vertical", fraction=0.046, pad=0.04)
# Trader
ax.scatter(**out["trader"])
ax.set_axis_off()
return solara.FigureMatplotlib(fig)
propertylayer_portrayal = {
"sugar": {
"color": "blue",
"alpha": 0.8,
"colorbar": True,
"vmin": 0,
"vmax": 10,
},
"spice": {"color": "red", "alpha": 0.8, "colorbar": True, "vmin": 0, "vmax": 10},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sanika-n The formatting here seems off --- are you using the ruff linter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I actually wrote spice in the same way as sugar but when I ran ruff it was changing the formatting of spice and it didn't allow me to keep it like sugar, so now I have made sugar formatted like spice...

}


sugarscape_space = make_mpl_space_component(
agent_portrayal=agent_portrayal,
propertylayer_portrayal=propertylayer_portrayal,
post_process=None,
draw_grid=False,
)

model_params = {
"seed": {
"type": "InputText",
Expand Down Expand Up @@ -73,7 +56,7 @@
page = SolaraViz(
model,
components=[
SpaceDrawer,
sugarscape_space,
make_plot_component("#Traders"),
make_plot_component("Price"),
],
Expand Down
Loading