使用 Matplotlib 动画更新 X 轴值

matplotlibpythondata visualization更新于 2025/9/12 5:32:17

要使用 Matplotlib 动画更新 X 轴值,我们可以采取以下步骤 −

  • 设置图形大小并调整子图之间和周围的填充。
  • 创建一个图形和一组子图。
  • 使用 numpy 创建 x 和 y 数据点。
  • 使用轴 (ax) 上的绘图方法绘制 x 和 y 数据点。
  • 通过反复调用根据帧设置 X 轴值的 animate 函数来制作动画。
  • 要显示图形,请使用 show() 方法。

示例

import matplotlib.pylab as plt
import matplotlib.animation as animation
import numpy as np

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

fig, ax = plt.subplots()
x = np.linspace(0, 15, 100)
y = np.sin(x)

ax.plot(x, y, lw=7)

def animate(frame):
   ax.set_xlim(left=0, right=frame)

ani = animation.FuncAnimation(fig, animate, frames=10)

plt.show()

输出


相关文章


有用资源