From befa6100856d6324987522a92e5ef9545b923672 Mon Sep 17 00:00:00 2001 From: sanika Date: Wed, 29 Jan 2025 22:12:04 +0530 Subject: [PATCH 1/2] first commit --- mesa/examples/advanced/sugarscape_g1mt/app.py | 59 +++++++------------ 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/mesa/examples/advanced/sugarscape_g1mt/app.py b/mesa/examples/advanced/sugarscape_g1mt/app.py index 678485ae33d..9e3d893edc9 100644 --- a/mesa/examples/advanced/sugarscape_g1mt/app.py +++ b/mesa/examples/advanced/sugarscape_g1mt/app.py @@ -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} - 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}, +} +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", @@ -73,7 +56,7 @@ def portray(g): page = SolaraViz( model, components=[ - SpaceDrawer, + sugarscape_space, make_plot_component("#Traders"), make_plot_component("Price"), ], From a9a17d5bc6c7b491b2744b9b81bbb04465e3cccf Mon Sep 17 00:00:00 2001 From: sanika Date: Fri, 31 Jan 2025 17:25:26 +0530 Subject: [PATCH 2/2] updated formatting --- mesa/examples/advanced/sugarscape_g1mt/app.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/mesa/examples/advanced/sugarscape_g1mt/app.py b/mesa/examples/advanced/sugarscape_g1mt/app.py index 9e3d893edc9..537336ef5ee 100644 --- a/mesa/examples/advanced/sugarscape_g1mt/app.py +++ b/mesa/examples/advanced/sugarscape_g1mt/app.py @@ -8,13 +8,7 @@ def agent_portrayal(agent): propertylayer_portrayal = { - "sugar": { - "color": "blue", - "alpha": 0.8, - "colorbar": True, - "vmin": 0, - "vmax": 10, - }, + "sugar": {"color": "blue", "alpha": 0.8, "colorbar": True, "vmin": 0, "vmax": 10}, "spice": {"color": "red", "alpha": 0.8, "colorbar": True, "vmin": 0, "vmax": 10}, }