Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matplotlib animation displays extra plot #414

Closed
ahwillia opened this issue Apr 19, 2016 · 4 comments
Closed

Matplotlib animation displays extra plot #414

ahwillia opened this issue Apr 19, 2016 · 4 comments

Comments

@ahwillia
Copy link

This is driving me a bit nuts. I'm using the following code to produce a simple animation. It works in the sense that the movie gets displayed but I also get an extra plot in my notebook, showing one of the frames of the movie.

Using IJulia.clear_output() only deletes the movie I want! The bonus frame stays around.

My guess is that this has something to do with PyCall (the equivalent example in a Jupyter notebook with a Python kernel works fine, so I'm convinced this is specific to Julia)

Code:

using PyCall
import IJulia
@pyimport matplotlib.animation as animation
using PyPlot

function showanim(filename)
    base64_video = base64encode(open(readbytes, filename))
    display("text/html", """<video controls src="data:video/x-m4v;base64,$base64_video">""")
end

A = randn(20,20,20)

fig = figure(figsize=(2,2))

function make_frame(i)
    imshow(A[:,:,i+1],interpolation="none")
end

anim = animation.FuncAnimation(fig, make_frame, frames=size(A,3), interval=20)
anim[:save]("test2.mp4", bitrate=-1, extra_args=["-vcodec", "libx264", "-pix_fmt", "yuv420p"]);
showanim("test2.mp4");

Screenshot:

screen shot 2016-04-19 at 3 23 36 pm

@JobJob
Copy link
Contributor

JobJob commented Apr 20, 2016

Hi, it's a bit kludgy, but I tried adding empty!(IJulia.displayqueue) just before the showanim and that works.

@stevengj should IJulia.clear_output possibly include empty!(displayqueue)? I have limited experience with clear_output so I'm not sure, but it helps for this particular case.

@ahwillia
Copy link
Author

Thanks, you are a hero. As a workaround I'll put empty!(IJulia.displayqueue) at the top of the showanim function.

Should I close this, or leave open in hopes of a less "kludgy" solution?

@JobJob
Copy link
Contributor

JobJob commented Apr 20, 2016

Good. I think we can leave it open and wait for @stevengj to weigh in.

@stevengj
Copy link
Member

stevengj commented Apr 20, 2016

Emptying the displayqueue on clear_output seems very reasonable.

As an alternative, you could also do

withfig(fig) do
    anim = animation.FuncAnimation(fig, make_frame, frames=size(A,3), interval=20)
    anim[:save]("test2.mp4", bitrate=-1, extra_args=["-vcodec", "libx264", "-pix_fmt", "yuv420p"])
end

The withfig function was designed for using PyPlot with Interact.jl animations, and what it does is to eliminate side effects from plotting (e.g. imshow will no longer push a figure to display).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants