Making GIFs in Julia
Code
Although the original Sakura Sushi restaurant tends to add much more detailed explanations, to emphasize how easy it is to create animated GIFs in Julia, we will keep this explanation as brief as possible.
Even setting aside simulating a random walk, creating an animated GIF like the one above can be very difficult and demanding, depending on the language. However, Julia makes this incredibly easy with the @animate
macro and gif()
function. The principle is simple. Attach the macro in front of a loop and draw the frames one by one as the loop executes. Once the frames are gathered into a variable, simply putting them into the gif()
function is all it takes. The fps
option allows you to set the speed of the animated GIF by specifying frames per second.
using Plots
random\_walk = cumsum(rand(100).-.5)
anim = @animate for t in 1:100
plot(random\_walk[1:t], legend = :none)
end; gif(anim, "example.gif", fps = 10)
Be mindful that if you don’t specify a different path, it will be saved in your documents. By making good use of this, one can create amazing animated GIFs like the one shown below.