3

I would like to create an animation of two axes. In the simple example illustrated below, I would like to plot two matrices using imshow as a function of time.

Coming from python, I would create an animation using matplotlib.animation similar to this:

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

A = randn(20,20,20,2)

fig, axes = PyPlot.subplots(nrows=1, ncols=2, figsize=(7, 2.5))
ax1, ax2 = axes

function make_frame(i)
    ax1.clear()
    ax2.clear()
    ax1.imshow(A[:,:,i+1, 1])
    ax2.imshow(A[:,:,i+1, 2])
end

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

This however just creates a blank animation. Do I need to use an init_func in the FuncAnimation? Do I need to enable blitting? Or can I update the artist using a set_data attribute?

  • Works fine for me - how are you viewing the output? – August Oct 25 '22 at 04:15
  • What platform are you running? Did you link an external python library to PyCall or an internal via Conda.jl ? Executing the above code as a file with julia (1.8.2) gives me a segmentation fault. – David Schlegel Oct 25 '22 at 12:52
  • I can get it to run and give the proper image if I run without the withfig(fig) surround (Julia 1.8.2, internal Python). With the withfig surround I get the segfault. – Bill Oct 25 '22 at 17:27

1 Answers1

1

Don't use IJulia for this routine. If you plan to include the routine in a notebook, just run the code and then view the file you create, without using withfig. withfig is causing your animation creation to abort for some reason, probably because it expects something within an IJulia environment to be set differently.

This works:

using PyCall
@pyimport matplotlib.animation as anim
using PyPlot

A = randn(20,20,20,2)
fig, axes = PyPlot.subplots(nrows=1, ncols=2, figsize=(7, 2.5))
ax1, ax2 = axes

function make_frame(i)
    ax1.clear()
    ax2.clear()
    ax1.imshow(A[:,:,i+1, 1])
    ax2.imshow(A[:,:,i+1, 2])
end

myanim = anim.FuncAnimation(fig, make_frame, frames=size(A,3), interval=20, blit=false)
myanim[:save]("test.mp4", bitrate=-1, extra_args=["-vcodec", "libx264", "-pix_fmt", "yuv420p"])
# now you can call your video viewer on "test.mp4"
Bill
  • 5,600
  • 15
  • 27
  • I can confirm that it actually works without using `withfig`. What is the deal with `withfig` then in the first place? Thanks for your answer. I think this solves it then. – David Schlegel Oct 26 '22 at 12:45
  • `withfig` is for using matplotlib in an IPython notebook when the plot is to be interactive, like a widget. Perhaps unless it is used specifically in such a context something in a relevant ipython DLL may throw an error? – Bill Oct 26 '22 at 18:27