# On-the-fly Animation in IJulia Notebooks

**URL:** <https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524>\
**Category:** Visualization\
**Created:** [November 23, 2016, 2:32pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524 "2016-11-23T14:32:42Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [November 24, 2016, 9:29am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/21 "2016-11-24T09:29:27Z")

</div>

yes, this one works great (I think aside from formatting it may actually be identical to the version I derived from @cstjean’s code above ). Thank you.

EDIT: not it only created a `gks.svg` not a `gks.mov`.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [November 24, 2016, 9:35am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/22 "2016-11-24T09:35:11Z")

</div>

Without the `clear_output` all the plots I am generating are put into the cell one after another.

If I do `default(show=true)` then depending on the backend I get various different behaviours that I don’t like. E.g. with GR I get an external window instead of inline, while with Plotlyjs I get the following:

 ![](https://global.discourse-cdn.com/julialang/original/3X/3/a/3aaa101c7760c373cd5a129604ab9afd5058aa59.png)

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [November 24, 2016, 12:08pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/23 "2016-11-24T12:08:34Z")

</div>

One more comment: the behaviour also seems to depend heavily on the browser. In Safari Plotlyjs works well in the way described above, while in Chrome it buffers and only shows the final result.

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [November 24, 2016, 5:28pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/24 "2016-11-24T17:28:35Z")

</div>

using PlotlyJS you may want to check out the `restyle!` function, which allows you to efficiently edit an existing plot. It looks like `setxy!` in Plots.jl should use that.

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [November 25, 2016, 8:53am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/25 "2016-11-25T08:53:46Z")

</div>

thank you - this also looks really useful.

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [December 1, 2016, 8:27pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/26 "2016-12-01T20:27:59Z")

</div>

I just added support for this in Plots (because I needed it ;))

See: [Plots Inline display in Jupyter - YouTube](https://www.youtube.com/watch?v=pLA9H9LzTWs&feature=youtu.be) and [https://github.com/tbreloff/Plots.jl/pull/597](https://github.com/tbreloff/Plots.jl/pull/597)

---

<div class="post-metadata">

**Author:** ![davide](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide/32/52136_2.png) [@davide](https://discourse.julialang.org/u/davide)\
**Post date:** [December 6, 2016, 2:09pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/27 "2016-12-06T14:09:09Z")

</div>

Is there a way to set the animation speed (e.g. redraw per seconds)?  
I am thinking of “replaying” data at the same speed they where acquired (or slower/faster) without first generating a video file.

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [December 6, 2016, 2:11pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/28 "2016-12-06T14:11:57Z")

</div>

You just need to sleep in the loop. It would be nice to have a macro that does this.

---

<div class="post-metadata">

**Author:** ![davide](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide/32/52136_2.png) [@davide](https://discourse.julialang.org/u/davide)\
**Post date:** [December 6, 2016, 3:09pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/29 "2016-12-06T15:09:25Z")

</div>

I see that the speed is limited by the re-plot (I am using Plots with the GR backend) and it is not constant (it slows down and speeds up; due to a cpu load?)

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [December 6, 2016, 3:40pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/30 "2016-12-06T15:40:11Z")

</div>

Do you see the same performance when you use GR’s external window instead of plotting within the notebook? I’ve found GR to be impressively fast and able to handle pretty high frame-rates for my real-time audio plotting stuff (plotting every 10ms or so), but I’ve never tried it inside the notebook.

---

<div class="post-metadata">

**Author:** ![davide](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide/32/52136_2.png) [@davide](https://discourse.julialang.org/u/davide)\
**Post date:** [December 6, 2016, 3:59pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/31 "2016-12-06T15:59:47Z")

</div>

I did not say that (sorry): I’m plotting in an external window (Julia REPL on Linux, on an i7 machine), not inside the notebook.  
It seems that GR.plot is faster (4 or 5 times faster) than GR through Plots.plot, and does not show any speed change during the animation.

---

<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:** [December 6, 2016, 7:16pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/32 "2016-12-06T19:16:55Z")

</div>

Using Plots with the gr() backend does not require any sleep cycles, e.g.

```julia
using Plots; gr(); default(show=:inline);
for i=1:10
    histogram(randn(10000))
end

```

---

<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:** [December 6, 2016, 7:21pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/33 "2016-12-06T19:21:42Z")

</div>

Is this a general impression, or did you observe this behavior with a specific plot type? The `import` and setup might be slower (with Plots), but drawing speed should not differ significantly.

If so, could you provide an example ?

---

<div class="post-metadata">

**Author:** ![davide](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide/32/52136_2.png) [@davide](https://discourse.julialang.org/u/davide)\
**Post date:** [December 7, 2016, 11:14am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/34 "2016-12-07T11:14:14Z")

</div>

I have a signal acquired at 200 Hz for almost 24 minutes, that is more than 284\_000 points.  
I import data through DataFrames.readtable(), getting a 2 columns (seconds and mV) dataframe .

I do not know if I use the more efficient way for the animations. Here is what I do.

**StatPlots**

(I use StatPlots but I checked the speed with Plots and is the same):

```julia
widthS = 120; # width of the recording to plot in the window (in seconds)
nPoints = widthS * 200 # number of points to plot

for n = 1:1:(length(data[:,2])-nPoints);
    StatPlots.plot(data[n:n+nPoints,:], :s, :mV) |> display
end

```

It took ~134 seconds for the animation to complete the first 30 seconds of the recording. If I add options to the plot (e.g. ylims, axis ticks, etc.) the speed further decreases.

**GR**

```julia
widthS = 120; # width of the recording to plot in the window (in seconds)
nPoints = widthS * 200 # number of points to plot

for n = 1:1:(length(data[:,2])-nPoints);
    GR.plot(data[n:n+nPoints,1], data[n:n+Points,2])
end

```

The GR.plot took ~38 seconds for the animation to complete the first 30 seconds (~3.5 times faster than Plots.plot.

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [December 7, 2016, 12:26pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/35 "2016-12-07T12:26:00Z")

</div>

You’re creating a brand new plot every time, which has overhead compared to  
a direct GR call. You should create the plot once and then update the  
series in the loop.

---

<div class="post-metadata">

**Author:** ![davide](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide/32/52136_2.png) [@davide](https://discourse.julialang.org/u/davide)\
**Post date:** [December 7, 2016, 7:31pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/36 "2016-12-07T19:31:36Z")

</div>

Ok, thanks. Any example?

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [December 7, 2016, 7:42pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/37 "2016-12-07T19:42:00Z")

</div>

julia\> p = plot(1)

julia\> for i=1:20  
p[1] = (1:10,rand(10))  
end; p

---

<div class="post-metadata">

**Author:** ![davide](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davide/32/52136_2.png) [@davide](https://discourse.julialang.org/u/davide)\
**Post date:** [December 8, 2016, 11:07am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/38 "2016-12-08T11:07:28Z")

</div>

Hi @tbreloff,

I probably miss something. I tried out your example but got no animation, just the plot of the final iteration (through the the “p” outside the loop): no updating occurs inside the loop.  
I tried to figure out how to get it working (e.g. with the “p” inside the loop) but with no success.  
What’s wrong? May you please point me to some documentation? How are plots updated?

Thanks

---

<div class="post-metadata">

**Author:** ![tbreloff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbreloff/32/68_2.png) [@tbreloff](https://discourse.julialang.org/u/tbreloff)\
**Post date:** [December 8, 2016, 11:20am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/39 "2016-12-08T11:20:31Z")

</div>

This was not a complete animation example, just a demo of updating the  
underlying data. I’m sure you can figure it out yourself if you think on it  
more.

---

<div class="post-metadata">

**Author:** ![hsseung](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hsseung/32/2521_2.png) [@hsseung](https://discourse.julialang.org/u/hsseung)\
**Post date:** [February 6, 2017, 3:01pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/40 "2017-02-06T15:01:26Z")

</div>

None of the examples above worked for me in IJulia, except those with PyPlot.  
I got the GR and Plots examples to work in IJulia only by adding a sleep(0.01) command.

[Previous page](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524.md?page=1)

[Next page](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524.md?page=3)
