Python matplotlib动态图

matplotlib动态图

  • 1.依赖python包
  • 2.使用matplotlib.animation.FuncAnimation
  • 3.使用celluloid.Camera
  • 参考文献

优美的动态图对写blog、展示ppt非常重要。

今天介绍了一款可视化动态图神器。

1.依赖python包

  • celluloid.Camera
  • matplotlib.animation
  • IPython.display.HTML

2.使用matplotlib.animation.FuncAnimation

from IPython.display import HTML
import matplotlib.pyplot as plt
import matplotlib.animation 
import numpy as np

t = np.linspace(0,2*np.pi)
x = np.sin(t)

fig, ax = plt.subplots()
ax.axis([0,2*np.pi,-1,1])
l, = ax.plot([],[])

def animate(i):
    l.set_data(t[:i], x[:i]);

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t));
HTML(ani.to_html5_video())

效果如下:
Python matplotlib动态图_第1张图片

3.使用celluloid.Camera

from IPython.display import HTML
import matplotlib.pyplot as plt
import matplotlib.animation 
import numpy as np
from celluloid import Camera

fig = plt.figure()
camera = Camera(fig)

for i in range(5):
    x=np.random.randint(0,10,6)
    y=np.random.randint(0,10,6)
    plt.scatter(x,y)

    camera.snap()

animation = camera.animate(blit=False, interval=100)
animation.save('./example.mp4', writer='ffmpeg', fps=20)
HTML(animation.to_html5_video())

效果如下:

Python matplotlib动态图_第2张图片

参考文献

[1] matplotlib.animation.FuncAnimation
[2] github/celluloid

你可能感兴趣的:(numpy,matplotlib,opencv,pandas,python,matplotlib,celluloid,动态图)