Problem with rendering animations with Plots.jl + GR backend

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:

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.

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?

Is this on Windows?

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.

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?

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

Few more info:

OS:

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

Julia info:

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.

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

gr(size = (1200, 800))

to this:

pyplot(size = (1200, 800))

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

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

Sure! I guess Plots.jl is the appropriate place for that, right?

Yes, that’s the right place.

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():

lay = grid(1, 2)

Now I get all the frames correctly rendered.