# Running 'scatter' from withing a function does nothing

**URL:** https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366
**Category:** New to Julia
**Created:** [September 28, 2023, 9:02pm UTC](https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366 "2023-09-28T21:02:48Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![ollie\_d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ollie_d/32/53134_2.png) [@ollie\_d](https://discourse.julialang.org/u/ollie_d)
#### Post date: [September 28, 2023, 9:02pm UTC](https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366/1 "2023-09-28T21:02:48Z")

</div>

I’ve made a function to start my simulation. If I run the line scatter(x,y) outside of the function it works fine and new dots appear. Within the function it runs but nothing appears. Do I always have to start the plot from outside a function? thanks

function run(time)  
xnext = xnow = xold = 0.0  
ynext = ynow = yold = 10.0  
g = -9.8

```
fps = 60
frames = fps*time
timestep = 1/fps
scatter(xnext,ynext) # nothing appears!
for i in range(1,1,frames)
    xnext, ynext = verlet_update(xnow,xold,ynow,yold,g,timestep)
    xold, yold = xnow, ynow
    xnow,ynow = xnext,ynext
    scatter!(xnow,ynow, markersize=20)
    sleep(timestep)
    println(ynext)
end

```

end

---

<div class="post-metadata">

### Author: ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)
#### Post date: [September 28, 2023, 9:20pm UTC](https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366/2 "2023-09-28T21:20:08Z")

</div>

Most (all?) plotting commands return a “figure” object; the plot is produced when your function returns that object, or when it is `display`ed explicitly. Try doing either

```julia
scatter!(xnow,ynow, markersize=20) |> display

```

or

```julia
f = scatter!(xnow,ynow, markersize=20)

```

and then return `f` at the end.

---

<div class="post-metadata">

### Author: ![ollie\_d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ollie_d/32/53134_2.png) [@ollie\_d](https://discourse.julialang.org/u/ollie_d)
#### Post date: [September 28, 2023, 10:22pm UTC](https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366/3 "2023-09-28T22:22:29Z")

</div>

thanks. returning f shows me that plot at the end which is ok but I was hoping to display an empty plot first and then update with each loop iteration

|\> display only prints the scatter type

---

<div class="post-metadata">

### Author: ![ollie\_d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ollie_d/32/53134_2.png) [@ollie\_d](https://discourse.julialang.org/u/ollie_d)
#### Post date: [September 28, 2023, 10:30pm UTC](https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366/4 "2023-09-28T22:30:20Z")

</div>

to ask more generally, I want to make a simulation of a bouncing ball and watch the ball. How should I structure my code when using GLmakie?

I’m trying to have a function run(time) that initialises the variables and the plot and then loops my verlet\_update function to move the ball.

---

<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: [September 28, 2023, 11:05pm UTC](https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366/5 "2023-09-28T23:05:16Z")

</div>

The [animation](https://docs.makie.org/stable/explanations/animation/index.html) documentation has a good example of this. The general idea is

```julia
    # Create an observable for your point
    pt = Observable(Point2f(xnext, ynext))
    # Create your plot
    fig = Figure()
    # Maybe display the figure if this is in a function and not an interactive session
    # display(fig)
    ax = Axis(fig[1,1])
    # plot the initial condition
    scatter!(ax, pt)
    # run the loop, updating the observable on each iteration
    for i in range(1,1,frames)
        xnext, ynext = verlet_update(xnow,xold,ynow,yold,g,timestep)
        xold, yold = xnow, ynow
        xnow,ynow = xnext,ynext
        # This updates the value of the point which automatically updates the scatter
        pt[] = Point2f(xnow, ynow)
        sleep(timestep)
        println(ynext)
    end

```

---

<div class="post-metadata">

### Author: ![ollie\_d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ollie_d/32/53134_2.png) [@ollie\_d](https://discourse.julialang.org/u/ollie_d)
#### Post date: [September 28, 2023, 11:27pm UTC](https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366/6 "2023-09-28T23:27:09Z")

</div>

thanks! and this has to be run at the root of the file or in a function? i’ll look through that page

---

<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: [September 28, 2023, 11:29pm UTC](https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366/7 "2023-09-28T23:29:28Z")

</div>

This can be run in a function, in that case be sure to call `display(fig)` before entering the loop to open the window.

---

<div class="post-metadata">

### Author: ![ollie\_d](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ollie_d/32/53134_2.png) [@ollie\_d](https://discourse.julialang.org/u/ollie_d)
#### Post date: [September 29, 2023, 12:00am UTC](https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366/8 "2023-09-29T00:00:29Z")

</div>

Great, thanks! Should I be using geometrybasics whenever possible?

---

<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: [September 29, 2023, 4:48am UTC](https://discourse.julialang.org/t/running-scatter-from-withing-a-function-does-nothing/104366/9 "2023-09-29T04:48:24Z")

</div>

I would say whenever appropriate. In this case, updating separate x and y observables would result in two updates of the plot, updating the point once ensures there is one update of the plot.

It is even more important when adding new points. There is no way to keep the scatter plot valid while separately adding x and y coordinates but adding Point objects can expand the plot and keep it valid.
