Skip to content

Commit

Permalink
jupyter_viz: Implement multiline plot
Browse files Browse the repository at this point in the history
Fixes projectmesa#1821.

This provides a declarative interface to plotting multiple lines in 1
chart.

Example usage for virus on network
```python
page = JupyterViz(
    VirusOnNetwork,
    model_params,
    measures=[
        # This is subject to change, as it could have been
        # {"Infected": {"color": "tab:red"}}, but let's keep it simple.
        {"Infected": "tab:red", "Susceptible": "tab:green", "Resistant": "tab:gray"},
        # Alternatively, if you don't want to manually specify colors:
        # ["Infected", "Susceptible", "Resistant"],
        make_text(get_resistant_susceptible_ratio),
    ],
    name="Virus Model",
    agent_portrayal=agent_portrayal,
```
  • Loading branch information
rht committed Jan 9, 2024
1 parent 4471047 commit c8c53c6
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions mesa/experimental/jupyter_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,17 @@ def make_plot(model, measure):
fig = Figure()
ax = fig.subplots()
df = model.datacollector.get_model_vars_dataframe()
ax.plot(df.loc[:, measure])
ax.set_ylabel(measure)
if isinstance(measure, str):
ax.plot(df.loc[:, measure])
ax.set_ylabel(measure)
elif isinstance(measure, dict):
for m, color in measure.items():
ax.plot(df.loc[:, m], label=m, color=color)
fig.legend()
elif isinstance(measure, (list, tuple)):
for m in measure:
ax.plot(df.loc[:, m], label=m)
fig.legend()
# Set integer x axis
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
solara.FigureMatplotlib(fig)
Expand Down

0 comments on commit c8c53c6

Please sign in to comment.