赞
踩
在 VS Code 中,可以通过以下步骤来确保动画正确显示:
配置 VS Code 使用适当的绘图后端:
VS Code 中使用 matplotlib 的动画功能,需要配置适当的绘图后端。推荐使用 TkAgg
或者 Qt5Agg
后端。你可以在代码中显式指定:
import matplotlib matplotlib.use('Qt5Agg') # 或者 'TkAgg' import matplotlib.pyplot as plt import numpy as np from matplotlib.animation import FuncAnimation # 创建图形和轴 fig, ax = plt.subplots() xdata, ydata = [], [] ln, = plt.plot([], [], 'r-', animated=True) # 初始化函数 def init(): ax.set_xlim(0, 2 * np.pi) ax.set_ylim(-1, 1) return ln, # 更新函数 def update(frame): xdata.append(frame) ydata.append(np.sin(frame)) ln.set_data(xdata, ydata) return ln, # 创建动画 ani = FuncAnimation(fig, update, frames=np.linspace(0, 2 * np.pi, 128), init_func=init, blit=True) # 显示动画 plt.show()
确保你安装了 GUI 后端库:
如果你使用的是 TkAgg
后端,你需要确保安装了 tkinter
。如果你使用的是 Qt5Agg
后端,你需要确保安装了 PyQt5
。
pip install pyqt5
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。