# 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:** 1

<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 23, 2016, 2:32pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/1 "2016-11-23T14:32:43Z")

</div>

I recently revisited how to do on-the-fly visualisation in an IJulia notebook, i.e., plotting results while the computation is still running. This was discussed a while back in [Google groups link](https://groups.google.com/forum/#!searchin/julia-users/Interactive%2420Animated%2420PyPlot%2420in%2420IJulia%7Csort:relevance/julia-users/Wb5hgyj2PMY/jtFVMvR01x0J)

I’ve tried the same scheme with `Plots.jl` with Plotly backend, and I am thrilled that this still works. (Julia 0.5.1pre). Below is the code for anybody interested.

But my key issue back then and still now is: why does it need to be so complicated? I.e. why can I not simply write

```julia
x = linspace(0, 2*pi, 100) |> collect 
dt, nsteps = 0.03, 300
for n = 1:nsteps
    plot(x, sin(x - dt*n))
end

```

Is there a fundamental barrier? I would really appreciate this functionality not just for my own research but also for teaching with Julia. (I can’t promise but I might even be able to find some funding to get it implemented.)

* * *

Here is the promised code now:

**CELL 1**

```julia
using Reactive, Interact, Plots
Plots.plotlyjs()

function myplot(data)    
    Plots.plot(data[1], data[2], xlims=(0, 2*pi), ylims=(-1.2,1.2))
end

x = linspace(0, 2*pi, 100) |> collect 
myinput = Signal((x, sin(x)))
lift(myplot, my input)

```

**CELL 2**

```julia
dt, nsteps = 0.03, 300
for n = 1:nsteps
    push!(myinput, (x, sin(x - n*dt)))
end

```

---

<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:** [November 23, 2016, 3:09pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/2 "2016-11-23T15:09:50Z")

</div>

I don’t understand… What “doesn’t work”? You probably just need to show  
the plot inside your loop. See the docs (specifically the page on “output”)

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [November 23, 2016, 4:46pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/3 "2016-11-23T16:46:37Z")

</div>

Hi cortner, I would use Plots.jl for anything fancy, but I sometimes use this solution for quick and dirty “animations”

```julia
using PyPlot
x = linspace(0, 2*pi, 100) |> collect 
dt, nsteps = 0.03, 30
fig = figure()
for n = 1:nsteps
    plot(x, sin(x - dt*n), animated=true)
    sleep(0.3)
    IJulia.clear_output(true)
    display(fig)
    clf()
end

```

---

<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 23, 2016, 5:42pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/4 "2016-11-23T17:42:51Z")

</div>

@tbreloff Thank you for pointing that out; embarrassingly I didn’t actually see this (I was only following the discussion groups to see whether somebody brings it up)

I now tried both `default(show=true)` and `display(plt)` and neither worked for me. With PyPlot nothing happens at all, while with Plotly it will overwrite the existing plot instead of replacing it. Probably there will be a way to fix either - I’ll go read you docs in detail. (But if somebody has seen this and knows the fix I’d love to hear it)

EDIT: if I use GR, then the kernel either crashes or I see nothing?

---

<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 23, 2016, 5:46pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/5 "2016-11-23T17:46:00Z")

</div>

@cstjean Thank you - this seems to work. Good to know there is such a simple solution.

The only problem with `IJulia.clear_output(true)` is that it creates very strong flickering, so not very useful for presentations.

---

<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:** [November 23, 2016, 7:22pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/6 "2016-11-23T19:22:14Z")

</div>

The following example works on macOS, Linux and Windows:

```julia
using GR
inline()
x = linspace(0, 2*pi, 100)
dt, nsteps = 0.03, 30
for n = 1:nsteps
    plot(x, sin(x - dt*n))
end

```

Can also be used in iTerm2 or Atom (`inline("iterm")`, `inline("atom")` resp.).

If you observe some flickering (in non-GUI mode), simple enable double buffering …

````julia
ENV["GKS_DOUBLE_BUF"]="true"
```

... before importing ``GR``.

Using ``gr()`` via ``Plots`` should also work.
````

---

<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 23, 2016, 7:41pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/7 "2016-11-23T19:41:16Z")

</div>

@jheinen Copy pasting your code into my notebook, I get no output at all. I am beginning to wonder whether there is something wrong with my installation . . .

---

<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:** [November 23, 2016, 8:54pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/8 "2016-11-23T20:54:29Z")

</div>

Sounds fishy. Update, build, and restart?

---

<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:** [November 23, 2016, 9:01pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/9 "2016-11-23T21:01:40Z")

</div>

Sorry. There is sth going wrong. Right now, it only works when creating a movie on the fly. That’s probably not what you want …

I’ll check that.

```julia
using GR
inline("mov")
x = linspace(0, 2*pi, 100)
dt, nsteps = 0.03, 30
for n = 1:nsteps
    plot(x, sin(x - dt*n))
end
GR.show()

```

---

<div class="post-metadata">

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

</div>

> GKS: /home/kjwiik/.julia/v0.5/GR/src/…/deps/gr/lib/./movplugin.so: undefined symbol: gks\_movplugin

---

<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:** [November 23, 2016, 9:44pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/11 "2016-11-23T21:44:20Z")

</div>

This problem is solved with the actual GR release …

```julia
Pkg.update()
ENV["GRDIR"]=""
Pkg.build("GR")

```

… should update GR to 0.18.0 and download the GR run-time (0.22.0).

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [November 23, 2016, 10:39pm UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/12 "2016-11-23T22:39:37Z")

</div>

Thanks, now there are no errors but the movie is empty…

 ![](https://global.discourse-cdn.com/julialang/original/3X/f/e/febbd0922fd7f2167024dd76dcd4c0c6d589faf6.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, 2:24am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/13 "2016-11-24T02:24:30Z")

</div>

I get the same result

---

<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, 2:32am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/14 "2016-11-24T02:32:01Z")

</div>

The following code, which modifies @cstjean’s suggestion works perfectly:

```julia
using Plots
Plots.plotlyjs()
x = linspace(0, 2*pi, 100) |> collect 
dt, nsteps = 0.03, 100
for n = 1:nsteps
    IJulia.clear_output(true)
    Plots.plot(x, sin(x - n*dt)) |> display
end

```

Replacing Plotly backend with GR works equally well. With PyPlot is just slow and flickers.

Thank you all for the help - I am very happy that this works.

---

<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, 2:37am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/15 "2016-11-24T02:37:12Z")

</div>

there is just one nagging little issue left: the notebook freezes during the animation (i.e. I can’t scroll, type or do anything else) Is this avoidable and if so where could I raise it as an issue?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [November 24, 2016, 7:18am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/16 "2016-11-24T07:18:32Z")

</div>

This should not be necessary. There must be some issue with the IJulia integration?

---

<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:** [November 24, 2016, 8:12am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/17 "2016-11-24T08:12:21Z")

</div>

So this is the code that works fine ith GR:

```julia
using GR
inline()
x = linspace(0, 2*pi, 100) |> collect 
dt, nsteps = 0.03, 100
for n = 1:nsteps
    IJulia.clear_output(true)
    plot(x, sin(x - n*dt)) |> display
end

```

There should be some GR option (in the next release) to avoid the `IJulia.clear_output()` call.

BTW: It seems to me, that the PlotlyJS solution is somehow buffered and not plotting in real-time.

---

<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, 8:28am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/18 "2016-11-24T08:28:14Z")

</div>

@mkborregaard What should not be necessary?

---

<div class="post-metadata">

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

</div>

the `clear_output()` call

---

<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:** [November 24, 2016, 9:13am UTC](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/20 "2016-11-24T09:13:53Z")

</div>

Did GR create a file `gks.mov` in your working directory? If so, can you play it using `ffplay` or `vlc`. If not, what system (OS, version) are you using?

Does the version I posted 1h ago work?

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