Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a53d8e8
separate setup functions from drawing functions
Sahil-Chhoker Nov 18, 2025
e39df00
simplify axes clearing
Sahil-Chhoker Nov 18, 2025
3f410d0
cleanup
Sahil-Chhoker Nov 18, 2025
238f33c
Revert "simplify axes clearing"
Sahil-Chhoker Nov 18, 2025
f23d486
cleanup-2
Sahil-Chhoker Nov 18, 2025
f88aff5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 20, 2025
09fe545
fix chart_props in network draw altair
Sahil-Chhoker Nov 20, 2025
edb3251
remove older method calls from canvas
Sahil-Chhoker Nov 20, 2025
077dee2
ensure backwards compatability
Sahil-Chhoker Nov 22, 2025
b8acbb5
update tests
Sahil-Chhoker Nov 22, 2025
def6a45
update virus_on_network
Sahil-Chhoker Nov 22, 2025
de29623
update schelling example
Sahil-Chhoker Nov 22, 2025
2faa078
update game_of_life example
Sahil-Chhoker Nov 22, 2025
70d447e
update boltzman_wealth_model example
Sahil-Chhoker Nov 22, 2025
7ba7a91
update bold_flockers example
Sahil-Chhoker Nov 22, 2025
953f0ca
cleanup and update wolf-sheep example
Sahil-Chhoker Nov 22, 2025
b23868c
update sugarscape example
Sahil-Chhoker Nov 22, 2025
b903326
update pd_grid example
Sahil-Chhoker Nov 22, 2025
7ae4afc
update civil_voilence example
Sahil-Chhoker Nov 22, 2025
8a22250
update tests inline with the new-API
Sahil-Chhoker Nov 22, 2025
d663821
Merge branch 'main' into viz-api-update
Sahil-Chhoker Nov 22, 2025
54e32df
nitpick improvements
Sahil-Chhoker Nov 22, 2025
d63ca10
change deprecation to pending deprecation warnings
Sahil-Chhoker Nov 25, 2025
d88b41b
Merge branch 'main' into viz-api-update
Sahil-Chhoker Nov 25, 2025
c2e6032
empty commit
Sahil-Chhoker Nov 25, 2025
a48c3d5
Merge branch 'viz-api-update' of https://github.com/Sahil-Chhoker/mes…
Sahil-Chhoker Nov 25, 2025
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
7 changes: 5 additions & 2 deletions mesa/examples/advanced/epstein_civil_violence/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ def post_process(ax):
)

epstein_model = EpsteinCivilViolence()
renderer = SpaceRenderer(epstein_model, backend="matplotlib")
renderer.draw_agents(citizen_cop_portrayal)
renderer = SpaceRenderer(epstein_model, backend="matplotlib").setup_agents(
citizen_cop_portrayal
)
# Specifically, avoid drawing the grid to hide the grid lines.
renderer.draw_agents()
renderer.post_process = post_process

page = SolaraViz(
Expand Down
6 changes: 5 additions & 1 deletion mesa/examples/advanced/pd_grid/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def pd_agent_portrayal(agent):
# Initialize model
initial_model = PdGrid()
# Create grid and agent visualization component using Altair
renderer = SpaceRenderer(initial_model, backend="altair").render(pd_agent_portrayal)
renderer = (
SpaceRenderer(initial_model, backend="altair")
.setup_agents(pd_agent_portrayal)
.render()
)

# Create visualization with all components
page = SolaraViz(
Expand Down
13 changes: 9 additions & 4 deletions mesa/examples/advanced/sugarscape_g1mt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ def post_process(chart):
# Here, the renderer uses the Altair backend, while the plot components
# use the Matplotlib backend.
# Both can be mixed and matched to enhance the visuals of your model.
renderer = SpaceRenderer(model, backend="altair").render(
agent_portrayal=agent_portrayal,
propertylayer_portrayal=propertylayer_portrayal,
post_process=post_process,
renderer = (
SpaceRenderer(model, backend="altair")
.setup_agents(agent_portrayal)
.setup_propertylayer(propertylayer_portrayal)
)
# Specifically, avoid drawing the grid to hide the grid lines.
renderer.draw_agents()
renderer.draw_propertylayer()

renderer.post_process = post_process

# Note: It is advised to switch the pages after pausing the model
# on the Solara dashboard.
Expand Down
4 changes: 2 additions & 2 deletions mesa/examples/advanced/wolf_sheep/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def post_process_lines(ax):
renderer = SpaceRenderer(
model,
backend="matplotlib",
)
renderer.draw_agents(wolf_sheep_portrayal)
).setup_agents(wolf_sheep_portrayal)
renderer.post_process = post_process_space
renderer.draw_agents()

page = SolaraViz(
model,
Expand Down
12 changes: 8 additions & 4 deletions mesa/examples/basic/boid_flockers/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ def boid_draw(agent):
model = BoidFlockers()

# Quickest way to visualize grid along with agents or property layers.
renderer = SpaceRenderer(
model,
backend="matplotlib",
).render(agent_portrayal=boid_draw)
renderer = (
SpaceRenderer(
model,
backend="matplotlib",
)
.setup_agents(boid_draw)
.render()
)

page = SolaraViz(
model,
Expand Down
13 changes: 9 additions & 4 deletions mesa/examples/basic/boltzmann_wealth_model/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ def post_process(chart):
# It builds the visualization in layers, first drawing the grid structure,
# and then drawing the agents on top. It uses a specified backend
# (like "altair" or "matplotlib") for creating the plots.
renderer = SpaceRenderer(model, backend="altair")
# Can customize the grid appearance.
renderer.draw_structure(grid_color="black", grid_dash=[6, 2], grid_opacity=0.3)
renderer.draw_agents(agent_portrayal=agent_portrayal, cmap="viridis", vmin=0, vmax=10)

renderer = (
SpaceRenderer(model, backend="altair")
.setup_structure( # To customize the grid appearance.
grid_color="black", grid_dash=[6, 2], grid_opacity=0.3
)
.setup_agents(agent_portrayal, cmap="viridis", vmin=0, vmax=10)
)
renderer.render()

# The post_process function is used to modify the Altair chart after it has been created.
# It can be used to add legends, colorbars, or other visual elements.
Expand Down
4 changes: 2 additions & 2 deletions mesa/examples/basic/conways_game_of_life/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def post_process(ax):
# Create initial model instance
model1 = ConwaysGameOfLife()

renderer = SpaceRenderer(model1, backend="matplotlib")
renderer = SpaceRenderer(model1, backend="matplotlib").setup_agents(agent_portrayal)
# In this case the renderer only draws the agents because we just want to observe
# the state of the agents, not the structure of the grid.
renderer.draw_agents(agent_portrayal=agent_portrayal)
renderer.draw_agents()
renderer.post_process = post_process

# Create the SolaraViz page. This will automatically create a server and display the
Expand Down
7 changes: 3 additions & 4 deletions mesa/examples/basic/schelling/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,11 @@ def agent_portrayal(agent):

# Note: Models with images as markers are very performance intensive.
model1 = Schelling()
renderer = SpaceRenderer(model1, backend="matplotlib")
renderer = SpaceRenderer(model1, backend="matplotlib").setup_agents(agent_portrayal)
# Here we use renderer.render() to render the agents and grid in one go.
# This function always renders the grid and then renders the agents or
# property layers on top of it if specified. It also supports passing the
# post_process function to fine-tune the plot after rendering in itself.
renderer.render(agent_portrayal=agent_portrayal)
# property layers on top of it if specified.
renderer.render()

HappyPlot = make_plot_component({"happy": "tab:green"})

Expand Down
16 changes: 10 additions & 6 deletions mesa/examples/basic/virus_on_network/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,16 @@ def post_process_lineplot(chart):


model1 = VirusOnNetwork()
renderer = SpaceRenderer(model1, backend="altair")
renderer.draw_structure(
node_kwargs={"color": "black", "filled": False, "strokeWidth": 5},
edge_kwargs={"strokeDash": [6, 1]},
) # Do this to draw the underlying network and customize it
renderer.draw_agents(agent_portrayal)
renderer = (
SpaceRenderer(model1, backend="altair")
.setup_structure( # Do this to draw the underlying network and customize it
node_kwargs={"color": "black", "filled": False, "strokeWidth": 5},
edge_kwargs={"strokeDash": [6, 1]},
)
.setup_agents(agent_portrayal)
)

renderer.render()

# Plot components can also be in altair and support post_process
StatePlot = make_plot_component(
Expand Down
23 changes: 6 additions & 17 deletions mesa/visualization/solara_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,19 +269,12 @@ def SpaceRendererComponent(
for artist in itertools.chain.from_iterable(all_artists):
artist.remove()

# Draw the space structure if specified
if renderer.space_mesh:
renderer.draw_structure(**renderer.space_kwargs)

# Draw agents if specified
renderer.draw_structure()
if renderer.agent_mesh:
renderer.draw_agents(
agent_portrayal=renderer.agent_portrayal, **renderer.agent_kwargs
)

# Draw property layers if specified
renderer.draw_agents()
if renderer.propertylayer_mesh:
renderer.draw_propertylayer(renderer.propertylayer_portrayal)
renderer.draw_propertylayer()

# Update the fig every time frame
if dependencies:
Expand All @@ -306,15 +299,11 @@ def SpaceRendererComponent(
propertylayer = renderer.propertylayer_mesh or None

if renderer.space_mesh:
structure = renderer.draw_structure(**renderer.space_kwargs)
structure = renderer.draw_structure()
if renderer.agent_mesh:
agents = renderer.draw_agents(
renderer.agent_portrayal, **renderer.agent_kwargs
)
agents = renderer.draw_agents()
if renderer.propertylayer_mesh:
propertylayer = renderer.draw_propertylayer(
renderer.propertylayer_portrayal
)
propertylayer = renderer.draw_propertylayer()

spatial_charts_list = [
chart for chart in [structure, propertylayer, agents] if chart
Expand Down
Loading