# Animating a 3D scatter plot in GLMakie

**URL:** <https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278>\
**Category:** General Usage\
**Tags:** visualization, makie, glmakie\
**Created:** [January 9, 2022, 12:01pm UTC](https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278 "2022-01-09T12:01:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 9, 2022, 12:01pm UTC](https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278/1 "2022-01-09T12:01:33Z")

</div>

Hi all,

I am trying to create an animation of a 3D scatter plot in GLMakie. In principle, I thought that it would be straightforward to adapt the 2D animation from the [documentation](https://makie.juliaplots.org/stable/documentation/animation/index.html), but this has proven to be more difficult than initially thought. There are two problems. First, the scatter plot is 2D instead of 3D. Second, it does not animate.

```julia
using GLMakie
points = Observable(Point3f[(0, 0, 0)])
fig, ax = scatter(points)

frames = 1:30

record(fig, "append_animation.mp4", frames;
        framerate = 30) do frame
    new_point = Point3f(frame, frame, frame)
    points[] = push!(points[], new_point)
end

```

---

<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:** [January 9, 2022, 12:09pm UTC](https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278/2 "2022-01-09T12:09:53Z")

</div>

The Axis is in 2D because the first point has a z coordinate of zero, so the heuristic that chooses 3d or 2d kind of fails. You can manually make an `LScene` or `Axis3`, though.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 9, 2022, 12:18pm UTC](https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278/3 "2022-01-09T12:18:40Z")

</div>

Thank you for your response. Unfortunately, I encounter the same problem with non-zero values:

```julia
using GLMakie

points = Observable(Point3f[(1, 1, 1)])

fig, ax = scatter(points)

```

---

<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:** [January 9, 2022, 12:28pm UTC](https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278/4 "2022-01-09T12:28:16Z")

</div>

Maybe I’m misremembering then, and it’s the extent in z that’s zero which causes this. We’ll probably change that to an input type heuristic in the future.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 9, 2022, 12:32pm UTC](https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278/5 "2022-01-09T12:32:35Z")

</div>

Is there a solution using Axis3? Its not clear to me how to properly construct it. I tried the following:

```julia
using GLMakie
points = Observable(Point3f[(1, 1, 1)])
fig, _ = scatter(points)
ax = Axis3(fig)
frames = 1:30

record(fig, "append_animation.mp4", frames;
        framerate = 30) do frame
    new_point = Point3f(frame, frame, frame)
    points[] = push!(points[], new_point)
end

```

It places a 3D plot inside a 2D plot (lower left corner).

---

<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:** [January 9, 2022, 1:20pm UTC](https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278/6 "2022-01-09T13:20:20Z")

</div>

```julia
fig = Figure()
ax = Axis3(fig[1, 1])
scat = scatter!(ax, ...)
...

```

Or

```julia
fig, ax3, scat = scatter(..., axis = (; type = Axis3))

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 9, 2022, 1:46pm UTC](https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278/7 "2022-01-09T13:46:37Z")

</div>

> [@Christopher\_Fisher](#):
>
> ```julia
> using GLMakie
> points = Observable(Point3f[(0, 0, 0)])
> fig, ax = scatter(points)
> 
> frames = 1:30
> 
> record(fig, "append_animation.mp4", frames;
> framerate = 30) do frame
> new_point = Point3f(frame, frame, frame)
> points[] = push!(points[], new_point)
> end
> 
> ```

Awsome. Thank you. That correctly creates a 3D scatter plot. However, the points still do not display:

```julia
points = Observable(Point3f[(.5, .5, .5)])

fig, ax, scat = scatter(points, axis = (;type=Axis3))

```

Did I do something incorrectly or is there a bug?

---

<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:** [January 9, 2022, 2:06pm UTC](https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278/8 "2022-01-09T14:06:33Z")

</div>

Oh well, there’s a long-standing bug with scatter markers in Axis3, for some reason they come out really really small. As a hacky workaround, you can set the markersize to something like 1000 to see them, the projection matrix of the axis is not compensated for correctly it seems, but I’m not too familiar with the sprite shader.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [January 9, 2022, 2:37pm UTC](https://discourse.julialang.org/t/animating-a-3d-scatter-plot-in-glmakie/74278/9 "2022-01-09T14:37:51Z")

</div>

Jules, thank you for your help. I added a working example below for anyone who might be interested.

```julia
using GLMakie
points = Observable(Point3f[(1, 1, 1)])
fig, ax, scat = scatter(points, axis = (;type=Axis3), markersize = 4000)
limits!(ax, 0, 30, 0, 30, 0, 30)

frames = 1:30

record(fig, "append_animation.mp4", frames;
        framerate = 30) do frame
    new_point = Point3f(frame, frame, frame)
    points[] = push!(points[], new_point)
end

```
