【发布时间】:2017-03-15 07:15:33
【问题描述】:
我想读取数据并绘制动态图,所以我用 PyQt5 学习了 matplotlib。我找到了示例,但它适用于 PyQt4 there。我将它修改为 PyQt5 但它有一些问题,当我点击开始按钮时它说错误
Traceback(最近一次调用最后一次):文件 "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/backend_bases.py", 第 1305 行,在 _on_timer ret = func(*args, **kwargs) 文件中 "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/animation.py", 第 1049 行,在 _step Still_going = Animation._step(self, *args) 文件中 "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/animation.py", 第 855 行,在 _step self._draw_next_frame(framedata, self._blit) 文件中 "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/animation.py", 第 873 行,在 _draw_next_frame self._pre_draw(framedata, blit) 文件中 "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/animation.py", 第 886 行,在 _pre_draw self._blit_clear(self._drawn_artists, self._blit_cache) 文件 "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/animation.py", 第 926 行,在 _blit_clear a.figure.canvas.restore_region(bg_cache[a]) KeyError:matplotlib.axes._subplots.AxesSubplot 对象位于 0x1067718d0
这是我的代码,有人可以帮助我吗?
import sys, os, random
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
import numpy as np
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.animation as animation
class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
# We want the axes cleared every time plot() is called
self.axes.hold(False)
self.compute_initial_figure()
#
FigureCanvas.__init__(self, fig)
self.setParent(parent)
def compute_initial_figure(self):
pass
class AnimationWidget(QWidget):
def __init__(self):
QMainWindow.__init__(self)
vbox = QVBoxLayout()
self.canvas = MyMplCanvas(self, width=5, height=4, dpi=100)
vbox.addWidget(self.canvas)
hbox = QHBoxLayout()
self.start_button = QPushButton("start", self)
self.stop_button = QPushButton("stop", self)
self.start_button.clicked.connect(self.on_start)
self.stop_button.clicked.connect(self.on_stop)
hbox.addWidget(self.start_button)
hbox.addWidget(self.stop_button)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.x = np.linspace(0, 5*np.pi, 400)
self.p = 0.0
self.y = np.sin(self.x + self.p)
self.line, = self.canvas.axes.plot(self.x, self.y, animated=True, lw=2)
def update_line(self, i):
self.p += 0.1
y = np.sin(self.x + self.p)
self.line.set_ydata(y)
return [self.line]
def on_start(self):
self.ani = animation.FuncAnimation(self.canvas.figure, self.update_line,
blit=True, interval=25)
def on_stop(self):
self.ani._stop()
if __name__ == "__main__":
qApp = QApplication(sys.argv)
aw = AnimationWidget()
aw.show()
sys.exit(qApp.exec_())
【问题讨论】:
标签: python animation matplotlib