# Automatic update in Makie with Observable of vector

**URL:** <https://discourse.julialang.org/t/automatic-update-in-makie-with-observable-of-vector/85623>\
**Category:** Visualization\
**Tags:** reactiveprogramming, makie\
**Created:** [August 11, 2022, 2:40pm UTC](https://discourse.julialang.org/t/automatic-update-in-makie-with-observable-of-vector/85623 "2022-08-11T14:40:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [August 11, 2022, 2:40pm UTC](https://discourse.julialang.org/t/automatic-update-in-makie-with-observable-of-vector/85623/1 "2022-08-11T14:40:26Z")

</div>

I need to iteratively call a Makie recipe according to an observable value.

Lets say I write a recipe `MyPlot` and I want to iteratively call `Scatter` inside.

```julia
using Makie

@recipe(MyPlot, x) do scene
    Theme(
        repeat = 3
    )
end

function Makie.plot!(myp::MyPlot)
    obs = @lift [$(myp.x) .* i for i in 1:$(myp.repeat)]

    hlines!(myp, 5, color=:red) # always plot a horizontal line

    for valrow in obs[]
        scatter!(myp, valrow) # depends on the attribute
    end

    myp
end

```

Works fine with first call:

```julia
julia> f,a,p = myplot([1,2,3]; repeat=5)

```

I get what I want

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/9/b90a2375397144fc612c8dbf676ab69d4d22182b.png)

But I cannot update it:

```julia
julia> p.repeat[] = 10 # doesn't update the figure
10

```

I know that theoretically I need to call the `scatter` recipe with an observable passed in, but I cannot wrap my head around it how to do it.

Any suggestions ?

_Please keep in mind this is just a minimal example for a more complicated code, where the iteration is necessary. So please don’t suggest solutions that involve calling `scatter` only once with `scatter!(myp, obs)` or similar._

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [August 11, 2022, 4:00pm UTC](https://discourse.julialang.org/t/automatic-update-in-makie-with-observable-of-vector/85623/2 "2022-08-11T16:00:49Z")

</div>

> [@filchristou](#):
>
> So please don’t suggest solutions that involve calling `scatter` only once with `scatter!(myp, obs)`

As far as I know, this is the solution to this problem. When I have had this come up in the past, I have used iteration to create an `Observable{Point2f[]}` that I then pass to `scatter!` once. In this simple example, that looks like

```julia
obs = @lift reduce(vcat, [Point2f.(i, $(myp.x) .* i) for i in 1:$(myp.repeat)])

```

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [August 11, 2022, 4:13pm UTC](https://discourse.julialang.org/t/automatic-update-in-makie-with-observable-of-vector/85623/3 "2022-08-11T16:13:05Z")

</div>

Thanks for the answer but this solution involves calling `scatter` only once, which is circumventing the problem.

In reality I don’t call `scatter` but `GraphMakie.EdgePlot` with different transparent colors, so that in the end a mixture of transparencies and colors is drawn.

If there is really no way around to get it working iteratively, I guess I should focus to somehow transform my data as you suggested for my problem as well…

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [August 11, 2022, 4:16pm UTC](https://discourse.julialang.org/t/automatic-update-in-makie-with-observable-of-vector/85623/4 "2022-08-11T16:16:20Z")

</div>

I was also thinking of transforming `Observable{Vector}` to a `Vector{Observable}` and pass this\* in `scatter`, but again it didn’t work.

\* each Observable

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [August 11, 2022, 4:50pm UTC](https://discourse.julialang.org/t/automatic-update-in-makie-with-observable-of-vector/85623/5 "2022-08-11T16:50:45Z")

</div>

I believe the `color=` kwarg for edgeplot can be a vector, so I would try creating two observables, one the vector of edges and one the vector of colors and then calling edgeplot with those.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [August 11, 2022, 8:28pm UTC](https://discourse.julialang.org/t/automatic-update-in-makie-with-observable-of-vector/85623/6 "2022-08-11T20:28:22Z")

</div>

You cannot have the existence of a plot depend on an observable, so you cannot have x scatter plots where x changes dynamically. At least not how things currently work. You can always concatenate the data and work with a single plot as suggested above, not sure why you don’t want to / can’t do that.

PS: Actually you can do it but you’d have to write more complex logic that adds and deletes the plot objects, renews observable connections etc. Probably not worth it
