mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Customize network layout, currently only spring layout

Open rmhopkins4 opened this issue 1 year ago • 3 comments

Currently, without implementing a custom drawer, networks are always drawn with the spring layout. A way to customize this would be well appreciated, whether it be in the agent_portrayal or in the model itself (which does somewhat violate the separation of model & visualization).

## matplotlib.py

# draws using networkx's matplotlib integration
def _draw_network_grid(space, space_ax, agent_portrayal):
    graph = space.G
    pos = nx.spring_layout(graph, seed=0)
    nx.draw(
        graph,
        ax=space_ax,
        pos=pos,
        **agent_portrayal(graph),
    )

rmhopkins4 avatar Aug 23 '24 15:08 rmhopkins4

What if there is an arbitrary kwargs that you can pass to nx.draw, ax.plot, and so on, to expose their full API?

rht avatar Aug 23 '24 17:08 rht

Oh, the layout has to be initialized separately from nx.draw, so this needs a separate kwarg.

rht avatar Aug 23 '24 17:08 rht

I got around this by just creating a custom viz: https://github.com/projectmesa/mesa-examples/blob/main/examples/aco_tsp/app.py#L39

zjost avatar Aug 24 '24 17:08 zjost