在Matplotlib实时绘图中移动X轴 要在Matplotlib实时绘图过程中移动X轴,我们可以执行以下步骤 – 设置图形大小并调整子图之间和周围的填充。 创建一个图和一组子图。 使用numpy创建x和y数据点。 使用plot()方法绘制x和y数据点。 通过重复调用函数animate()创建动画,该函数在实时绘图期间移动X轴。 要显示图形,请使用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() PythonCopy