|
14 | 14 | "metadata": {},
|
15 | 15 | "source": [
|
16 | 16 | "If you want to get straight to the tutorial checkout these environment providers:<br>\n",
|
17 |
| - "[](https://mybinder.org/v2/gh/projectmesa/mesa/main?labpath=docs%2Ftutorials%2F4_visualization_basic.ipynb) (This can take 30 seconds to 5 minutes to load)\n", |
| 17 | + "[](https://mybinder.org/v2/gh/projectmesa/mesa/main?labpath=docs%2Ftutorials%2F5_visualization_dynamic_agents.ipynb) (This can take 30 seconds to 5 minutes to load)\n", |
18 | 18 | "\n",
|
19 | 19 | "Due to conflict with Colab and Solara there are no colab links for this tutorial\n",
|
20 | 20 | "\n",
|
|
60 | 60 | "\n",
|
61 | 61 | "import mesa\n",
|
62 | 62 | "from mesa.discrete_space import CellAgent, OrthogonalMooreGrid\n",
|
63 |
| - "from mesa.visualization import SolaraViz, make_plot_component, make_space_component" |
| 63 | + "from mesa.visualization import SolaraViz, SpaceRenderer, make_plot_component\n", |
| 64 | + "from mesa.visualization.components import AgentPortrayalStyle" |
64 | 65 | ]
|
65 | 66 | },
|
66 | 67 | {
|
|
189 | 190 | "\n",
|
190 | 191 | "In the first visualization, all we could see is the agents moving around -- but not how much money they had, or anything else of interest. In this tutorial let's change it so that agents are represented by the units of wealth they have. So those who are broke (wealth 0) are drawn in red, smaller. \n",
|
191 | 192 | "\n",
|
192 |
| - "As Mesa is open source, it is important to point out that currently, we can't direct the drawing order of the circles, so a broke agent may be overshadowed by a wealthy agent. If you have some ideas, please feel free to [contribute](https://github.com/projectmesa/mesa/blob/main/CONTRIBUTING.md). \n", |
| 193 | + "Since Mesa is open source, if you have ideas to improve the visualization stack, feel free to [contribute](https://github.com/projectmesa/mesa/blob/main/CONTRIBUTING.md).\n", |
| 194 | + "\n", |
| 195 | + "When using the default drawer, an agent's shape can be customized in addition to its size and color.\n", |
| 196 | + "\n", |
| 197 | + "* For `matplotlib`, allowed shape values can be found [here](https://matplotlib.org/stable/api/markers_api.html).\n", |
| 198 | + "* For `altair`, supported shapes include: `\"circle\"`, `\"square\"`, `\"cross\"`, `\"diamond\"`, `\"triangle-up\"`, `\"triangle-down\"`, `\"triangle-right\"`, and `\"triangle-left\"`.\n", |
| 199 | + "\n", |
| 200 | + "**Note:**\n", |
| 201 | + "Always check which backend is being used before assigning shapes. If a shape name doesn't match what's supported by the selected backend, it may cause errors.\n", |
| 202 | + "\n", |
| 203 | + "In some cases, Mesa implicitly converts common shape names between Altair and Matplotlib (e.g., from Altair to Matplotlib), but this mapping is limited—and there's no support for conversion in the reverse direction (Matplotlib to Altair).\n", |
193 | 204 | "\n",
|
194 |
| - "In addition to size and color, an agent's shape can also be customized when using the default drawer. The allowed values for shapes can be found [here](https://matplotlib.org/stable/api/markers_api.html).\n", |
195 | 205 | "\n",
|
196 | 206 | "To do this, we go back to our `agent_portrayal` code and add some code to change the portrayal based on the agent properties and launch the server again."
|
197 | 207 | ]
|
|
203 | 213 | "outputs": [],
|
204 | 214 | "source": [
|
205 | 215 | "def agent_portrayal(agent):\n",
|
206 |
| - " size = 10\n", |
207 |
| - " color = \"tab:red\"\n", |
| 216 | + " portrayal = AgentPortrayalStyle(size=50, color=\"tab:orange\")\n", |
208 | 217 | " if agent.wealth > 0:\n",
|
209 |
| - " size = 50\n", |
210 |
| - " color = \"tab:blue\"\n", |
211 |
| - " return {\"size\": size, \"color\": color}" |
| 218 | + " portrayal.update((\"color\", \"tab:blue\"), (\"size\", 100))\n", |
| 219 | + " return portrayal" |
212 | 220 | ]
|
213 | 221 | },
|
214 | 222 | {
|
|
244 | 252 | "source": [
|
245 | 253 | "Then just like last time we instantiate the visualization object which (by default) displays the grid containing the agents, and timeseries of values computed by the model's data collector. In this example, we specify the Gini coefficient.\n",
|
246 | 254 | "\n",
|
247 |
| - "There are 3 buttons:\n", |
| 255 | + "There are 3 main buttons (we will discuss the play interval, render interval and use threads in lesson 6):\n", |
248 | 256 | "- the step button, which advances the model by 1 step\n",
|
249 | 257 | "- the play button, which advances the model indefinitely until it is paused\n",
|
250 | 258 | "- the pause button, which pauses the model\n",
|
|
263 | 271 | "source": [
|
264 | 272 | "# Create initial model instance\n",
|
265 | 273 | "money_model = MoneyModel(n=50, width=10, height=10)\n",
|
| 274 | + "renderer = SpaceRenderer(model=money_model, backend=\"matplotlib\").render(\n", |
| 275 | + " agent_portrayal=agent_portrayal\n", |
| 276 | + ")\n", |
266 | 277 | "\n",
|
267 |
| - "SpaceGraph = make_space_component(agent_portrayal)\n", |
268 | 278 | "GiniPlot = make_plot_component(\"Gini\")\n",
|
269 | 279 | "\n",
|
270 | 280 | "page = SolaraViz(\n",
|
271 | 281 | " money_model,\n",
|
272 |
| - " components=[SpaceGraph, GiniPlot],\n", |
| 282 | + " renderer,\n", |
| 283 | + " components=[GiniPlot],\n", |
273 | 284 | " model_params=model_params,\n",
|
274 | 285 | " name=\"Boltzmann Wealth Model\",\n",
|
275 | 286 | ")\n",
|
|
292 | 303 | "source": [
|
293 | 304 | "## Next Steps\n",
|
294 | 305 | "\n",
|
295 |
| - "Check out the next [visualization tutorial custom components](https://mesa.readthedocs.io/latest/tutorials/6_visualization_custom.html) on how to further enhance your interactive dashboard." |
| 306 | + "Check out the next tutorial [visualization rendering with `SpaceRenderer`](https://mesa.readthedocs.io/latest/tutorials/6_visualization_rendering_with_space_renderer.html) on how to further enhance your interactive dashboard." |
296 | 307 | ]
|
297 | 308 | },
|
298 | 309 | {
|
|
308 | 319 | "metadata": {
|
309 | 320 | "anaconda-cloud": {},
|
310 | 321 | "kernelspec": {
|
311 |
| - "display_name": "Python 3 (ipykernel)", |
| 322 | + "display_name": "env", |
312 | 323 | "language": "python",
|
313 | 324 | "name": "python3"
|
314 | 325 | },
|
|
322 | 333 | "name": "python",
|
323 | 334 | "nbconvert_exporter": "python",
|
324 | 335 | "pygments_lexer": "ipython3",
|
325 |
| - "version": "3.12.5" |
| 336 | + "version": "3.13.1" |
326 | 337 | },
|
327 | 338 | "widgets": {
|
328 | 339 | "state": {},
|
|
0 commit comments