{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"using Plots"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Animations in Julia with Plots\n",
"\n",
"Reference: https://docs.juliaplots.org/latest/animations/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A quick intro to animations using `Plots` in Julia. It turns out it's super simple. \n",
"\n",
"You can animate any plot by adding the `@animate` or `@gif` macros to a loop. Let's do an example without any abstractions like recipes or whatever and get right to it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`@animate` gets added to the beginning of a loop, and what it does is it saves the plot output at each iteration into a temporary .png file. Each of these snapshots form an individual frame of your animation. \n",
"\n",
"You can then call the `gif` function to display the frames of your animation as a repeating gif:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Info: Saved animation to \n",
"│ fn = /Users/nelsontang/repos/julia-animation/tmp.gif\n",
"└ @ Plots /Users/nelsontang/.julia/packages/Plots/Xv2qA/src/animation.jl:114\n"
]
},
{
"data": {
"text/html": [
""
],
"text/plain": [
"Plots.AnimatedGif(\"/Users/nelsontang/repos/julia-animation/tmp.gif\")"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Simple dataset of 50 random numbers\n",
"y = rand(50)\n",
"\n",
"# Use the @animate macro in front of a loop to create each frame of your animation\n",
"anim = @animate for i in 1:50\n",
" # At each iteration we will plot a new subset of the data\n",
" # I set the xlimits and ylimits so the x/y axes don't autofit as the data grows\n",
" plot(y[1:i], \n",
" xlims=(1,50),\n",
" ylims=(0,1))\n",
"end\n",
"\n",
"# Save the animation as a gif and display it\n",
"gif(anim)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we can already see the gif repeating in an infinite loop and it doesn't look very appealing. You can set the `fps` attribute to set the frames per second a little slower. And by default it saves this as `tmp.gif` in the local directory, you can change the filename to something else with the second argument:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Info: Saved animation to \n",
"│ fn = /Users/nelsontang/repos/julia-animation/new_animation.gif\n",
"└ @ Plots /Users/nelsontang/.julia/packages/Plots/Xv2qA/src/animation.jl:114\n"
]
},
{
"data": {
"text/html": [
""
],
"text/plain": [
"Plots.AnimatedGif(\"/Users/nelsontang/repos/julia-animation/new_animation.gif\")"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gif(anim, \"new_animation.gif\", fps=12)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That's it! To animate something in base plots you simply call the `@animate` macro in front of a loop, and within each loop you create each frame of the animation. \n",
"\n",
"And for convenience you can combine the `@animation for...end |> gif` call into a `@gif` call:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Info: Saved animation to \n",
"│ fn = /Users/nelsontang/repos/julia-animation/tmp.gif\n",
"└ @ Plots /Users/nelsontang/.julia/packages/Plots/Xv2qA/src/animation.jl:114\n"
]
},
{
"data": {
"text/html": [
""
],
"text/plain": [
"Plots.AnimatedGif(\"/Users/nelsontang/repos/julia-animation/tmp.gif\")"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"@gif for i in 1:50\n",
" plot(y[1:i], xlims=(1,50), ylims=(0,1))\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.8.3",
"language": "julia",
"name": "julia-1.8"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}