Conor
Conor

Reputation: 731

How to Plot Several 3D Trajectories in Julia (using a for-loop, presumably?) -- also, how to animate over time?

I am trying to plot a set of 3D trajectories in a single plot in Julia. By 3-D trajectories I mean: different sets of 3-D coordinates over time. These trajectories are stored in a multidimensional array called positions, where the dimensions respectively correspond to the Trajectory ID, X-Y-Z coordinate and Time. For example, positions[75,2,1:100] refers to the Y (2nd) coordinate of the 75th Trajectory, across the first 100 timesteps of the trajectory.

I am trying to figure out why the following code doesn't work:

using Plots

plotlyjs()

time_indices = 1:100

ax= scatter3d(positions[1,1,time_indices],positions[1,2,time_indices],positions[1,3,time_indices],label="Trajectory 1 for times 1 to 100")

for n in 2:size(positions,1)
    scatter3d!(ax, positions[n,1,time_indices], positions[n,2,time_indices],positions[n,3,time_indices],label="Trajectory $n for times 1 to 100")
end

When I run that code, I don't see anything in the Plots window (I'm using Atom), although I don't get any errors / it appears to run successfully. Any thoughts on what I'm doing wrong? Should I use a different backend? It doesn't work on either gr() or plotlyjs() (those are the only ones I know of, based on tutorials I've completed).

Follow up question: once I can successfully plot such 3-D trajectories in a single static plot, I am wondering how you would go about animating them over time (using @gif or @animate macros, presumably)? I am asking this here, because I wasn't able to understand the documentation / tutorial on 3D animations, unfortunately. Googling / other sources have also not helped :(

Upvotes: 2

Views: 1082

Answers (2)

Conor
Conor

Reputation: 731

How to do the second bit (the animation - see details in comments on the accepted answer):

gr()
anim = @animate for t in time_indices
    all_positions_t = positions[:,:,t] # positions of all trajectories at time t
    scatter3d(all_positions_t[:,1], all_positions_t[:,2],all_positions_t[:,3],label="")
end
gif(anim, "some_file_name.gif", fps=15)

Upvotes: 1

cbk
cbk

Reputation: 4370

It looks like you're just missing a display(ax) at the end, after the loop.

Edit: to animate, try

anim = @animate for n in 1:size(positions,1)
    scatter3d(positions[n,1,time_indices], positions[n,2,time_indices],positions[n,3,time_indices],label="Trajectory $n for times 1 to 100")
end
gif(anim, "some_file_name.gif", fps=15)

(or if you want the prior trajectories to show up as well in later frames of the gif, then replace the scatter3d with scatter3d!)

I can't test the above without having your positions, but here is another example that I have just tested on Julia 1.5.0beta with Plots and the GR backend:

using Plots; gr();
anim = @animate for i=1:100
    plot(sin.(range(0,i/10*pi,length=1000)), label="", ylims=(-1,1))
end
gif(anim, "anim_fps15.gif", fps=15)

Upvotes: 2

Related Questions