12

Is there any way to use moving plot without ffmpeg?

import matplotlib.animation as animation
from IPython.display import HTML

fig, ax = plt.subplots(figsize=(15, 8))
animator = animation.FuncAnimation(fig, draw_barchart, frames=range(1968, 2019))
HTML(animator.to_jshtml()) 
animator.save('dynamic_images.mp4')

My code is above, I am getting the key error.mp4', ValueError: unknown file extension: .mp4

I tried installing conda install -c conda-forge ffmpeg end up with SSL issue

  • Is there any way to use moving plot without ffmpeg

  • As like error throwing is there any way to use 'matplotlib.animation.PillowWriter'

Disclaimer : I went through the link https://www.wikihow.com/Install-FFmpeg-on-Windows but the URL is blocked by the IT team

2 Answers2

12

This is my frist answer on SO, so please be gentle.
The answer above seems like a good idea but it doesn't answer why the matplotlib built-in method doesn't work, so I decided to add some explanation.

This might be a duplicate (possible duplicate?).
The issue is that you're missing ffmpeg.
If you are using conda as your manager then install the missing packages.
You could also do that using apt, dnf, pacman or whatever else you are using.

In case you're using conda:
conda install -c conda-forge ffmpeg
ffmpeg on anaconda.org

Probably you're going to miss another dependency, namely Openh264.
Again if using conda: conda install -c conda-forge openh264
Openh264 on anaconda.org

If you're still missing more dependencies, resolving them will depend on the platform you are using.
If you are using Windows I would recommend trying miniconda out.

In my case, after installing openh264 using conda (my platform was/is Fedora) I still encountered problems because the installed lib was called libopenh264.so.6 instead of libopenh264.so.5.
I decided to try it out anyway and created a symbolic link to the version ending with 6 in the same directory (/lib64/) and it worked.

nlhnt
  • 173
  • 2
  • 11
6

You can save animated plot as .gif with use of celluloid library:

from matplotlib import pyplot as plt
from celluloid import Camera
import numpy as np


# create figure object
fig = plt.figure()
# load axis box
ax = plt.axes()
# set axis limit
ax.set_ylim(0, 1)
ax.set_xlim(0, 10)

camera = Camera(fig)
for i in range(10):
    ax.scatter(i, np.random.random())
    plt.pause(0.1)
    camera.snap()

animation = camera.animate()
animation.save('animation.gif', writer='PillowWriter', fps=2)

Output:

enter image description here

Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38