Skip to content

Remove the legacy space drawing API - #4130

Merged
EwoutH merged 1 commit into
mesa:mainfrom
EwoutH:remove-legacy-space-drawing
Nov 18, 2026
Merged

EwoutH merged 1 commit into
mesa:mainfrom
EwoutH:remove-legacy-space-drawing

Conversation

@EwoutH

@EwoutH EwoutH commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Summary

Removes the space drawing API from before Mesa 3.3 from the 4.0 branch. SpaceRenderer has replaced it since 3.3, and 3.5.3 deprecates it (#4064). Following #3330, main gets the removal directly rather than a backport of the deprecation.

Removed:

  • make_space_component, make_mpl_space_component, make_altair_space
  • SpaceMatplotlib, SpaceAltair
  • the make_space_matplotlib and make_space_altair aliases
  • the mesa.visualization.mpl_space_drawing module: draw_space, the per-space helpers (draw_orthogonal_grid, draw_hex_grid, …), and its collect_agent_data

Motive

Cleanup!

Implementation

  • MatplotlibBackend imported _to_numpy_argument_array from mpl_space_drawing. That helper and _coerce_color_array now live in backends/matplotlib_backend.py, and their tests moved to test_backends.py.
  • SolaraViz(components="default") built its space with make_altair_space. It now creates an Altair SpaceRenderer with a default AgentPortrayalStyle (agent id as tooltip), unless a renderer is passed. The renderer is created once with solara.use_memo, so re-renders don't replace it and model resets still go through copy_renderer. The docstring said this option was the default; it isn't (the default is []), so that's corrected too.
  • altair_components.py and matplotlib_components.py now only hold the plot components.

Usage Examples

# Before
SolaraViz(model, components=[make_space_component(agent_portrayal)])

# After
renderer = SpaceRenderer(model, backend="matplotlib").setup_agents(agent_portrayal).render()
SolaraViz(model, renderer)

The migration guide has a new 4.0 entry covering this and the draw_space → renderer.canvas case.

Additional Notes

  • pd_grid/analysis.ipynb drew on its own subplot axes with draw_space, which SpaceRenderer has no public way to do. It now draws the moves with imshow. The notebook was already broken on main (old PdGrid(50, 50, "Sequential", rng=...) constructor and model.run), so it now uses PrisonersDilemmaScenario and run_for as well. I executed it, and it runs and plots correctly.
  • Docs: overview.md and tutorial 10 now use SpaceRenderer; the API page drops mpl_space_drawing. Tutorial 10 executes. It still emits the existing dict-portrayal FutureWarning, which is unrelated to this PR.
  • Tests: legacy drawing tests are removed; test_examples_viz.py renders through SpaceRendererComponent; a new test covers components="default" (Altair renderer, and an explicit renderer takes precedence).
  • pytest tests/ -Werror -Wdefault::PendingDeprecationWarning: 1174 passed. The 9 Playwright tests in test_examples_viz.py couldn't start a browser locally, so CI runs those; I rendered the new space component for all nine example models without a browser, before and after stepping, without errors. Pre-commit passes (Vale skipped locally).
  • Open PR Draw HexGrid agents at their hexagon in legacy draw_hex_grid #4043 fixes the legacy draw_hex_grid on main, which this PR deletes; it would only still apply to 3.5.x-maintenance.

SpaceRenderer has replaced the pre-3.3 space drawing API since Mesa 3.3,
and 3.5.3 deprecates it (mesa#4064). Remove it for Mesa 4.0:

- make_space_component, make_mpl_space_component, make_altair_space,
  SpaceMatplotlib, SpaceAltair, and the make_space_matplotlib and
  make_space_altair aliases
- the mesa.visualization.mpl_space_drawing module (draw_space and the
  per-space helpers); the array helpers the matplotlib backend shared with
  it move into the backend

SolaraViz(components="default") now draws the space with an Altair
SpaceRenderer instead of make_altair_space.

The pd_grid analysis notebook drew on its own subplot axes with
draw_space. It now draws the moves with imshow, and uses the current
PdGrid constructor and run_for, so it runs again. Tutorial 10, the
overview and the tests use SpaceRenderer, and the migration guide has
an entry for the removal.

Assisted-by: Claude Opus 5.5
@EwoutH EwoutH added the breaking Release notes label label Nov 16, 2026
@github-actions github-actions Bot added docs Release notes label example Changes the examples or adds to them. testing Release notes label visualization labels Nov 16, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -4.3% [-5.2%, -3.4%] 🔵 +0.8% [+0.5%, +1.0%]
BoltzmannWealth large 🔵 -3.6% [-4.5%, -2.8%] 🔵 -3.7% [-7.0%, -0.1%]
Schelling small 🔵 -2.3% [-3.2%, -1.4%] 🔵 -0.7% [-1.5%, +0.1%]
Schelling large 🔵 -1.4% [-2.8%, -0.1%] 🔵 -1.7% [-4.5%, +1.3%]
WolfSheep small 🔵 +0.1% [-0.3%, +0.5%] 🔵 -1.1% [-3.8%, +1.4%]
WolfSheep large 🔵 +0.3% [-0.1%, +0.8%] 🔵 -0.4% [-2.1%, +1.1%]
SugarscapeG1mt small 🔵 -1.6% [-2.6%, -0.5%] 🔵 -0.1% [-1.7%, +1.6%]
SugarscapeG1mt large 🔵 -0.3% [-1.3%, +1.0%] 🔵 -2.4% [-4.1%, -0.3%]
BoidFlockers small 🔵 -0.0% [-0.4%, +0.4%] 🔵 +0.8% [+0.5%, +1.1%]
BoidFlockers large 🔵 +0.6% [+0.0%, +1.1%] 🔵 +0.6% [+0.5%, +0.9%]
MultiLevelAllianceModel small 🔵 -0.1% [-1.3%, +0.9%] 🔵 -0.6% [-1.1%, -0.2%]
MultiLevelAllianceModel large 🔵 -1.2% [-2.3%, -0.2%] 🔵 -4.7% [-6.7%, -2.5%]

@EwoutH
EwoutH merged commit 7787437 into mesa:main Nov 18, 2026
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Release notes label docs Release notes label example Changes the examples or adds to them. testing Release notes label visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants