Skip to content

Commit

Permalink
solara: Implement visualization for network grid (#1767)
Browse files Browse the repository at this point in the history
  • Loading branch information
rht authored Aug 17, 2023
1 parent 1ee5cda commit 24a1df3
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion mesa/experimental/jupyter_viz.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import threading

import matplotlib.pyplot as plt
import networkx as nx
import reacton.ipywidgets as widgets
import solara
from matplotlib.figure import Figure
from matplotlib.ticker import MaxNLocator

import mesa

# Avoid interactive backend
plt.switch_backend("agg")

Expand Down Expand Up @@ -91,10 +94,24 @@ def portray(self, g):
return out


def _draw_network_grid(viz, space_ax):
graph = viz.model.grid.G
pos = nx.spring_layout(graph, seed=0)
nx.draw(
graph,
ax=space_ax,
pos=pos,
**viz.agent_portrayal(graph),
)


def make_space(viz):
space_fig = Figure()
space_ax = space_fig.subplots()
space_ax.scatter(**viz.portray(viz.model.grid))
if isinstance(viz.model.grid, mesa.space.NetworkGrid):
_draw_network_grid(viz, space_ax)
else:
space_ax.scatter(**viz.portray(viz.model.grid))
space_ax.set_axis_off()
solara.FigureMatplotlib(space_fig, dependencies=[viz.model, viz.df])

Expand Down

0 comments on commit 24a1df3

Please sign in to comment.