# How to create a gif file of plot?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-gif-file-of-plot/99795>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [June 3, 2023, 1:22am UTC](https://discourse.julialang.org/t/how-to-create-a-gif-file-of-plot/99795 "2023-06-03T01:22:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moslem\_Uddin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moslem_uddin/32/49483_2.png) [@Moslem\_Uddin](https://discourse.julialang.org/u/Moslem_Uddin)\
**Post date:** [June 3, 2023, 1:22am UTC](https://discourse.julialang.org/t/how-to-create-a-gif-file-of-plot/99795/1 "2023-06-03T01:22:26Z")

</div>

I’m a new user of JULIA. I’m trying to line by line translation of the following MATLAB code

```julia
t=0;
dt =1e-4;
tmax=0.5;
nmax=round(tmax/dt);

for n=1:nmax
    t=t+dt;
if mod(n,100)==0
    x = 0:0.1:2*pi;
    y = sin(x + (n/2));
   plot(x, y)
       title(sprintf('time=%1.4f',t))
    pause(0.1)
end
end

```

which gives the following output  
 ![singif](https://global.discourse-cdn.com/julialang/original/3X/4/d/4d3938c36f10a5f8e25791087bd44684eb6a1dce.gif)  
My attempt for JULIA code is as follow:

```julia
using Plots
t=0
dt =1e-4;
tmax=0.5;
nmax=round(tmax/dt);
p=plot()
for n=1:nmax
    t=t+dt
if mod(n,100)==0
    x = 0:0.1:2π
    y = sin.(x .+ (n/2))
   plot!(x, y, xlims=(0,2π), ylims=(-1,1),legend=false,fmt=png)
   title!("Time = $t")
    sleep(0.1)
end
end
p

```

which gives  
 ![sin](https://global.discourse-cdn.com/julialang/original/3X/8/c/8cc5cf6acc7902ca998115bdc05d45bfcff3fd0b.png)  
I want to have a gif file like MATLAB with same code.  
Your help is appreciated

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [June 3, 2023, 5:19am UTC](https://discourse.julialang.org/t/how-to-create-a-gif-file-of-plot/99795/2 "2023-06-03T05:19:56Z")

</div>

```julia
ENV["GKS_WSTYPE"] = "mp4"

using GR

function main()
    t = 0
    dt = 1e-4
    tmax = 0.5
    nmax = round(tmax / dt)
    for n = 1:nmax
        t = t + dt
        if mod(n, 100) == 0
            x = 0:0.1:2π
            y = sin.(x .+ (n / 2))
            plot(x, y, xlim=(0,2π), ylim=(-1,1), title="Time = $t", grid=false)
        end
    end
end

main()

```

---

<div class="post-metadata">

**Author:** ![Moslem\_Uddin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moslem_uddin/32/49483_2.png) [@Moslem\_Uddin](https://discourse.julialang.org/u/Moslem_Uddin)\
**Post date:** [June 3, 2023, 6:39pm UTC](https://discourse.julialang.org/t/how-to-create-a-gif-file-of-plot/99795/3 "2023-06-03T18:39:07Z")

</div>

The above in Julia REFL giving

 ![Screenshot 2023-06-03 133444](https://global.discourse-cdn.com/julialang/original/3X/8/3/83f27edf2144711657bd2b6320df3716318cefc9.png)  
not a movie.

Also in Jupyter notebook its not giving any output.

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [June 4, 2023, 4:47am UTC](https://discourse.julialang.org/t/how-to-create-a-gif-file-of-plot/99795/4 "2023-06-04T04:47:46Z")

</div>

Did you set the environment before importing GR?

`ENV["GKS_WSTYPE"] = "mp4"`

---

<div class="post-metadata">

**Author:** ![aryavorskiy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aryavorskiy/32/43466_2.png) [@aryavorskiy](https://discourse.julialang.org/u/aryavorskiy)\
**Post date:** [June 4, 2023, 6:50pm UTC](https://discourse.julialang.org/t/how-to-create-a-gif-file-of-plot/99795/5 "2023-06-04T18:50:04Z")

</div>

What you probably want to do is this:

```julia
using Plots
t=0
dt =1e-4;
tmax=0.5;
nmax=round(tmax/dt);
anim = Animation()
for n=1:nmax
    t=t+dt
    if mod(n,100)==0
        x = 0:0.1:2π
        y = sin.(x .+ (n/2))
        plot(x, y, xlims=(0,2π), ylims=(-1,1),legend=false,title="Time = $t")
        frame(anim)
    end
end
gif(anim, "lol.gif", fps=10)

```

In short, you need to create a special Animation object and load the frames into it. Refer to [this manual](https://docs.juliaplots.org/latest/animations/) for more info.

I hope my answer will be helpful.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [June 5, 2023, 10:18am UTC](https://discourse.julialang.org/t/how-to-create-a-gif-file-of-plot/99795/6 "2023-06-05T10:18:32Z")

</div>

Plots is a great package. Some, myself included, prefer Makie. It is a litt more involded to get started with, but offers a lot of flexibility.

The documentation for creating videos in make can be found [here]([Animations · Makie](https://docs.makie.org/stable/documentation/animation/#animations_using_observables), where I have linked to the section that uses `Observables` to animate a wave with changing time. It should be simple to change the code so that it is the frequency that changes.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [June 5, 2023, 10:22am UTC](https://discourse.julialang.org/t/how-to-create-a-gif-file-of-plot/99795/7 "2023-06-05T10:22:30Z")

</div>

Note also that often, creating the video file can take a long time. If I only want to see an animation, and do not need to share the file, I generally love to change the value of an observable in a loop, and just watch the figure as it changes. If you want to do this, use the GLMakie backend, and run `Makie.inline(false)` to ensure that you get a standalone plotting window. The plot pane is VSCode does not update when Observables change unless you use WGLMakie, which I generally find to be less responsive and slower than GLMake.
