# Agents.jl: Interactive plotting - agent traits controllable on a slider

**URL:** <https://discourse.julialang.org/t/agents-jl-interactive-plotting-agent-traits-controllable-on-a-slider/92735>\
**Category:** Modelling & Simulations\
**Tags:** question, agents\
**Created:** [January 10, 2023, 12:21am UTC](https://discourse.julialang.org/t/agents-jl-interactive-plotting-agent-traits-controllable-on-a-slider/92735 "2023-01-10T00:21:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sea\_slug](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sea_slug/32/52907_2.png) [@sea\_slug](https://discourse.julialang.org/u/sea_slug)\
**Post date:** [January 10, 2023, 12:21am UTC](https://discourse.julialang.org/t/agents-jl-interactive-plotting-agent-traits-controllable-on-a-slider/92735/1 "2023-01-10T00:21:53Z")

</div>

I want to generate an interactive plot for the [wolf-sheep model](https://juliadynamics.github.io/Agents.jl/stable/examples/predator_prey/). I want to make it so that agent traits can be controlled on an interactive slider, for example:  
the initial number of sheep (`n_sheep`) vary between 1 and 20  
the probability of sheep reproducing (`sheep_reproduce`) vary between 0.01 and 0.1 by 0.01 intervals

From what I can see in the “Interactive plotting” [tutorial](https://juliadynamics.github.io/Agents.jl/stable/agents_visualizations/#Interactive-ABM-Applications-1), only model properties can be made changeable on a slider.

I tried the following (from the interactive plotting tutorial):

> params = Dict(  
> :n\_sheep =\> 1:20,  
> )
> 
> fig, ax, abmobs = abmplot(sheepwolfgrass;  
> agent\_step! = sheepwolf\_step!,  
> model\_step! = grass\_step!,  
> params,  
> plotkwargs…)  
> fig

And `n_sheep` shows up as a slider but when you push the “update” button it gives the error: `KeyError: key "n_sheep" not found`.

I then added `n_sheep = n_sheep` to `properties`, but that didn’t work either.

My question: how to you add agent traits such as `sheep_reproduce` or `n_sheep` (not strictly an agent trait, but not a model property either) as sliders when generating interactive plots?

Any advice would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [January 10, 2023, 12:45am UTC](https://discourse.julialang.org/t/agents-jl-interactive-plotting-agent-traits-controllable-on-a-slider/92735/2 "2023-01-10T00:45:31Z")

</div>

> [@sea\_slug](#):
>
> :n\_sheep =\> 1:20,

The Dict key is a Symbol, with a preceding colon. Check that you’re using that for your lookup key?

---

<div class="post-metadata">

**Author:** ![sea\_slug](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sea_slug/32/52907_2.png) [@sea\_slug](https://discourse.julialang.org/u/sea_slug)\
**Post date:** [January 10, 2023, 1:22am UTC](https://discourse.julialang.org/t/agents-jl-interactive-plotting-agent-traits-controllable-on-a-slider/92735/3 "2023-01-10T01:22:36Z")

</div>

Yep I am. When I include `:n_sheep` in `properties`, pressing the “update” button no longer produces the `KeyError: key "n_sheep" not found` error, but the initial number of sheep doesn’t change either (the button does nothing).

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [January 10, 2023, 12:08pm UTC](https://discourse.julialang.org/t/agents-jl-interactive-plotting-agent-traits-controllable-on-a-slider/92735/4 "2023-01-10T12:08:00Z")

</div>

> [@sea\_slug](#):
>
> how to you add agent traits such as `sheep_reproduce` or `n_sheep` (not strictly an agent trait, but not a model property either) as sliders when generating interactive plots?

As you can see [here](https://juliadynamics.github.io/Agents.jl/stable/examples/predator_prey/#Making-the-model-1) the `n_sheep` property is only used during initialisation of the model. When you use the interactive application provided by `InteractiveDynamics.jl`, you already provide an _existing model_ to the plotting function, e.g. `fig, ax, abmobs = abmplot(sheepwolfgrass; agent_step! = sheepwolf_step!, model_step! = grass_step!, plotkwargs...)`. This means that whenever you click the `reset model` button, you just recreate the exact same model that you’ve had when you started the interactive app. 🙂 (That’s also the reason why the button is called “reset model” and not “new model” or something similar.)

What you want to do is to create a whole new model on the fly and replace it in the interactive plot. That’s not possible right now. Instead you can just as easily create a new model in the REPL and run the `abmplot` function again. It’s almost instantaneous so there’s no real downside to it except that you don’t do it via a graphical interface.

Having said that, your expectations regarding the functionality make sense to me. I’ll have a look if I can find the time to implement it.

---

<div class="post-metadata">

**Author:** ![sea\_slug](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sea_slug/32/52907_2.png) [@sea\_slug](https://discourse.julialang.org/u/sea_slug)\
**Post date:** [January 10, 2023, 9:26pm UTC](https://discourse.julialang.org/t/agents-jl-interactive-plotting-agent-traits-controllable-on-a-slider/92735/5 "2023-01-10T21:26:51Z")

</div>

Thank you for the clear explanation, that makes sense and I agree it is not too difficult to launch a new instance of the model.

If that functionality could be implemented that would be great! Being able to instantaneously tweak initial conditions and see how the model responds would be helpful for model debugging/testing.

Thank you for the fantastic tools!

---

<div class="post-metadata">

**Author:** ![NebulaComplex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nebulacomplex/32/214696_2.png) [@NebulaComplex](https://discourse.julialang.org/u/NebulaComplex)\
**Post date:** [February 4, 2025, 7:02pm UTC](https://discourse.julialang.org/t/agents-jl-interactive-plotting-agent-traits-controllable-on-a-slider/92735/7 "2025-02-04T19:02:21Z")

</div>

Any plans to update the functionality re:discussion?

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [February 4, 2025, 9:05pm UTC](https://discourse.julialang.org/t/agents-jl-interactive-plotting-agent-traits-controllable-on-a-slider/92735/8 "2025-02-04T21:05:04Z")

</div>

the functionality is desired so you are very welcomed to contribute it via a Pull Request. There isn’t anybody working on this at the moment as far as I know.

---

<div class="post-metadata">

**Author:** ![NebulaComplex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nebulacomplex/32/214696_2.png) [@NebulaComplex](https://discourse.julialang.org/u/NebulaComplex)\
**Post date:** [February 6, 2025, 2:34am UTC](https://discourse.julialang.org/t/agents-jl-interactive-plotting-agent-traits-controllable-on-a-slider/92735/9 "2025-02-06T02:34:49Z")

</div>

I’ll take a crack at it over the summer, maybe; my group is interested in getting the GUI up to the level of Netlogos for pedagogical purposes. But, big ups to the Agents.jl team for the great work; we dig it (modelers@ucmerced).
