如何在 Matplotlib 中对线图进行动画处理?
为了在 matplotlib 中对线图进行动画处理,我们可以采取以下步骤 -
使用 subplots() 方法创建一个图形和一套子图。
限制 x 和 y 轴的刻度。
使用 numpy 创建 x 和 t 数据点。
从坐标向量中返回坐标矩阵 X2 和 T2。
使用 plot() 方法通过 x 和 F 数据点绘制一条线。
为了制作动画图,更新 y 数据。
通过反复调用函数 *func*、current fig、animate 和 interval 来制作动画。
使用 show() 方法来显示图形。
示例
import numpy as np
from matplotlib import pyplot as plt, animation
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
ax.set(xlim=(-3, 3), ylim=(-1, 1))
x = np.linspace(-3, 3, 91)
t = np.linspace(1, 25, 30)
X2, T2 = np.meshgrid(x, t)
sinT2 = np.sin(2 * np.pi * T2 / T2.max())
F = 0.9 * sinT2 * np.sinc(X2 * (1 + sinT2))
line, = ax.plot(x, F[0, :], color='k', lw=2)
def animate(i):
line.set_ydata(F[i, :])
anim = animation.FuncAnimation(fig, animate, interval=100, frames=len(t) - 1)
anim.save('503.gif')
plt.show()输出
当我们执行此代码时,它将显示带动画的线图。

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