Skip to content

Instantly share code, notes, and snippets.

@rht
Created May 10, 2023 08:28
Show Gist options
  • Save rht/e0a63e1cae232a49d8142eb1952c0bd2 to your computer and use it in GitHub Desktop.
Save rht/e0a63e1cae232a49d8142eb1952c0bd2 to your computer and use it in GitHub Desktop.
from matplotlib.figure import Figure
from boltzmann_wealth_model.model import BoltzmannWealthModel
import panel as pn
pn.extension("ipywidgets")
def mpl_plot(avg):
fig = Figure()
ax = fig.add_subplot()
avg.plot(ax=ax)
return fig
def mpl_imshow(g):
fig = Figure()
ax = fig.add_subplot()
ax.imshow(g)
return fig
n_agents_default = 10
width = 10
height = 10
model = BoltzmannWealthModel(n_agents_default, width, height)
def agent_portrayal(content):
# assert len(content) <= 1
if len(content) == 0:
return 0
agent = content[0]
if agent.wealth > 0:
return 2
return 1
def portray(g):
return [[agent_portrayal(subcontent) for subcontent in content] for content in g]
grid = mpl_imshow(portray(model.grid._grid))
mpl_pane = pn.pane.Matplotlib(grid)
gini_plot = mpl_plot(model.datacollector.get_model_vars_dataframe())
plot_pane = pn.pane.Matplotlib(gini_plot)
button_step = pn.widgets.Button(name="Step", button_type="primary")
button_reset = pn.widgets.Button(name="Reset", button_type="primary")
def do_step(event):
model.step()
gini = model.datacollector.get_model_vars_dataframe()
gini_plot = mpl_plot(gini)
plot_pane.object = gini_plot
grid = mpl_imshow(portray(model.grid._grid))
mpl_pane.object = grid
def do_reset(event):
global model
model = BoltzmannWealthModel(n_agents.value, width, height)
button_step.on_click(do_step)
button_reset.on_click(do_reset)
# n_steps = pn.widgets.IntSlider(name="Number of Steps", value=50, start=1, end=100)
n_agents = pn.widgets.IntSlider(name="Number of Agents", value=50, start=1, end=100)
# interactive = pn.bind(run, steps=n_steps, width=width, height=height, view=mpl_plot)
# first_app = pn.Column(n_steps, n_agents, width, height, interactive)
first_app = pn.Row(pn.Column(n_agents), pn.Column(button_step, button_reset, mpl_pane, plot_pane))
first_app.show(open=False, port=8521)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment