在实时绘图期间在 Matplotlib 中移动 X 轴

pythonserver side programmingprogramming更新于 2025/8/8 18:20:17

要在实时绘图期间在 Matplotlib 中移动 X 轴,我们可以采取以下步骤 −

  • 设置图形大小并调整子图之间和周围的填充。
  • 创建一个图形和一组子图。
  • 使用 numpy 创建 x 和 y 数据点。
  • 使用 plot() 方法绘制 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.cos(x)

ax.plot(x, y, lw=2, color='red')

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

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

plt.show()

输出


相关文章


有用资源