# Problem with rendering animations with Plots.jl + GR backend

**URL:** <https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661>\
**Category:** Visualization\
**Tags:** plots, gr, animations\
**Created:** [July 5, 2017, 12:00pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661 "2017-07-05T12:00:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![CrocoDuck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crocoduck/32/250_2.png) [@CrocoDuck](https://discourse.julialang.org/u/CrocoDuck)\
**Post date:** [July 5, 2017, 12:00pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/1 "2017-07-05T12:00:45Z")

</div>

Hi there!

I have been trying to get a gif file demonstrating the comb filtering resulting from delayed sinusoidal signals superposition. This is the code:

```julia
using Plots

gr(size = (1200, 800))

Fₛ = 96000 # Sample Rate [Hz]     
δ = 0.001 # Lag [s]
fₛ = 20 # Starting frequency [Hz]
fₑ = 1000 # End frequency [Hz]
τ = 1 / fₛ # Time width to display [s]
F = 500 # Number of frames

f = linspace(fₛ, fₑ, F)
t = linspace(0, τ, 1000)

x = zeros(t)
y = zeros(t)
z = zeros(t)
H = zeros(f)
lay = grid(1, 2)
anim = Animation()

for n = 1:length(f)

    x = cospi.(2 * f[n] * t)
    y = cospi.(2 * f[n] * (t - δ))
    z = x + y
    H[n] = maximum(abs, z)
    
    plot(
    plot(t, [x y z], 
    label = ["Primary" "Delayed by $δ seconds" "Sum"], 
    ylim = (-2, 2), 
    title = "Frequency: $(@sprintf("%.2f", f[n])) Hz", 
    xlabel = "Time [s]",
    ylabel = "Amplitude [normalized]"),
    plot(f, H,
    label = "Comb Filter",
    ylim = (0, 2),
    title = "Amplitude as function of frequency",
    xlabel = "Frequency [Hz]",
    ylabel = "Amplitude [normalized]"),
    layout = lay)

    frame(anim)
    
end

```

When I go ahead and create a gif out of the results all the frames are white except the first one.

```julia
gif(anim, "/tmp/anim_fps15.gif", fps = 15)

```

The weird thing is that the code above will work for simple plots (i.e. without subplots, for example just the plot showing the cosine waves).

Am I doing something wrong or did I hit a bug?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 5, 2017, 1:06pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/2 "2017-07-05T13:06:18Z")

</div>

Is this on Windows?

---

<div class="post-metadata">

**Author:** ![CrocoDuck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crocoduck/32/250_2.png) [@CrocoDuck](https://discourse.julialang.org/u/CrocoDuck)\
**Post date:** [July 5, 2017, 1:54pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/3 "2017-07-05T13:54:10Z")

</div>

Hi! No, I am on Arch Linux, Julia straight from the repos, packages are fresh installed. If needed I can give more info about versions when I get home.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 5, 2017, 2:03pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/4 "2017-07-05T14:03:09Z")

</div>

> [@CrocoDuck](#):
>
> If needed I can give more info about versions when I get home.

That would be helpful. I am trying to isolate it from a few known bugs. I don’t know of any for the situation you’re talking about, so I’m not sure what’s going on. Does the PyPlot backend animate fine?

---

<div class="post-metadata">

**Author:** ![CrocoDuck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crocoduck/32/250_2.png) [@CrocoDuck](https://discourse.julialang.org/u/CrocoDuck)\
**Post date:** [July 5, 2017, 2:05pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/5 "2017-07-05T14:05:22Z")

</div>

> [@ChrisRackauckas](#):
>
> Does the PyPlot backend animate fine?

I haven’t actually installed that, I will try.

---

<div class="post-metadata">

**Author:** ![CrocoDuck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crocoduck/32/250_2.png) [@CrocoDuck](https://discourse.julialang.org/u/CrocoDuck)\
**Post date:** [July 5, 2017, 5:54pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/6 "2017-07-05T17:54:45Z")

</div>

Few more info:

OS:

```julia
Linux arch 4.9.35-1-lts #1 SMP Thu Jun 29 15:22:34 CEST 2017 x86_64 GNU/Linux

```

Julia info:

```julia
julia> versioninfo(true)
Julia Version 0.6.0
Commit 903644385b* (2017-06-19 13:05 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i3-5010U CPU @ 2.10GHz
  WORD_SIZE: 64
  uname: Linux 4.9.35-1-lts #1 SMP Thu Jun 29 15:22:34 CEST 2017 x86_64 unknown
Memory: 7.719829559326172 GB (4303.28125 MB free)
Uptime: 280.0 sec
Load Avg: 0.8876953125 0.80810546875 0.3798828125
Intel(R) Core(TM) i3-5010U CPU @ 2.10GHz: 
       speed user nice sys idle irq
#1 2000 MHz 3621 s 664 s 607 s 22805 s 0 s
#2 1999 MHz 4804 s 639 s 575 s 21581 s 0 s
#3 1999 MHz 3314 s 752 s 564 s 23116 s 0 s
#4 1999 MHz 3339 s 834 s 583 s 22833 s 0 s

  BLAS: libblas
  LAPACK: liblapack
  LIBM: libm
  LLVM: libLLVM-4.0.0 (ORCJIT, broadwell)
Environment:
  MOZ_PLUGIN_PATH = /usr/lib/mozilla/plugins
  HOME = /home/crocoduck
  WINDOWPATH = 2
  TERM = xterm-256color
  ANT_HOME = /usr/share/apache-ant
  PATH = /usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/opt/mpich/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
  PKG_CONFIG_PATH = :/opt/mpich/lib/pkgconfig

Package Directory: /home/crocoduck/.julia/v0.6
4 required packages:
 - GR 0.22.0
 - PlotlyJS 0.6.3
 - Plots 0.12.0
 - Rsvg 0.1.0
44 additional packages:
 - BaseTestNext 0.2.2
 - BinDeps 0.6.0
 - Blink 0.5.3
 - Cairo 0.3.0
 - Codecs 0.3.0
 - ColorTypes 0.5.1
 - Colors 0.7.3
 - Compat 0.26.0
 - DataArrays 0.5.3
 - DataFrames 0.10.0
 - DataStructures 0.5.3
 - DocStringExtensions 0.3.3
 - FileIO 0.4.1
 - FixedPointNumbers 0.3.8
 - FixedSizeArrays 0.2.5
 - GZip 0.3.0
 - Graphics 0.2.0
 - Gtk 0.13.0
 - Hiccup 0.1.1
 - HttpCommon 0.2.7
 - HttpParser 0.2.0
 - HttpServer 0.2.0
 - JSON 0.12.0
 - Juno 0.3.0
 - LaTeXStrings 0.2.1
 - Lazy 0.11.7
 - MacroTools 0.3.7
 - MbedTLS 0.4.5
 - Measures 0.1.0
 - Media 0.3.0
 - Mustache 0.1.4
 - Mux 0.2.3
 - NaNMath 0.2.5
 - PlotThemes 0.1.4
 - PlotUtils 0.4.2
 - RecipesBase 0.2.0
 - Reexport 0.0.3
 - SHA 0.3.3
 - Showoff 0.1.1
 - SortingAlgorithms 0.1.1
 - SpecialFunctions 0.1.1
 - StatsBase 0.17.0
 - URIParser 0.1.8
 - WebSockets 0.2.3

```

I am gonna install and try PyPlot right now.

---

<div class="post-metadata">

**Author:** ![CrocoDuck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crocoduck/32/250_2.png) [@CrocoDuck](https://discourse.julialang.org/u/CrocoDuck)\
**Post date:** [July 5, 2017, 6:15pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/7 "2017-07-05T18:15:35Z")

</div>

PyPlot installed and all packages updated. In the code, I just changed this

```julia
gr(size = (1200, 800))

```

to this:

```julia
pyplot(size = (1200, 800))

```

The resulting gif still has only the first frame correctly plotted.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 5, 2017, 6:16pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/8 "2017-07-05T18:16:42Z")

</div>

I’m not sure what’s going on. File an issue for this.

---

<div class="post-metadata">

**Author:** ![CrocoDuck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crocoduck/32/250_2.png) [@CrocoDuck](https://discourse.julialang.org/u/CrocoDuck)\
**Post date:** [July 5, 2017, 6:19pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/9 "2017-07-05T18:19:40Z")

</div>

> [@ChrisRackauckas](#):
>
> I’m not sure what’s going on. File an issue for this.

Sure! I guess [Plots.jl](https://github.com/JuliaPlots/Plots.jl/issues) is the appropriate place for that, right?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [July 5, 2017, 6:25pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/10 "2017-07-05T18:25:54Z")

</div>

Yes, that’s the right place.

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [July 6, 2017, 1:16am UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/11 "2017-07-06T01:16:25Z")

</div>

The outer `plot` method mutates the layout argument, so you need to refresh it for every iteration (or just use a copy). This is not specific to animations.

---

<div class="post-metadata">

**Author:** ![CrocoDuck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crocoduck/32/250_2.png) [@CrocoDuck](https://discourse.julialang.org/u/CrocoDuck)\
**Post date:** [July 6, 2017, 5:55pm UTC](https://discourse.julialang.org/t/problem-with-rendering-animations-with-plots-jl-gr-backend/4661/12 "2017-07-06T17:55:42Z")

</div>

> [@Ralph\_Smith](#):
>
> The outer plot method mutates the layout argument, so you need to refresh it for every iteration (or just use a copy). This is not specific to animations.

Hi Ralph, thank you for your tip.

I put this line in the for loop, just before plot():

```julia
lay = grid(1, 2)

```

Now I get all the frames correctly rendered.
