在 Matplotlib 中创建 D3.js 动画的动画 GIF 文件
为了从D3.js动画创建动画 GIF 文件,我们可以执行以下步骤−
- 设置图表大小并调整子图之间和周围的填充。
- 创建一个新的图表或激活一个现有的图表。
- 将轴添加到当前图表并使其成为当前轴。
- 用空列表绘制一条线。
- 为了初始化线条,传递空列表。
- 为了对正弦曲线进行动画,更新正弦曲线值并返回线条实例。
- 使用PillowWriter()类获得一个电影写入器实例。
- 使用PillowWriter保存 .gif 文件。
示例
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
ani = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True)
writer = animation.PillowWriter(fps=25)
ani.save("sine.gif", writer=writer)输出


广告
数据结构
网络
关系型数据库
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP