森栏
我删除了与本教程无关的所有内容。然后,我开始将代码集成到教程代码中,直到它崩溃为止。这突出了我的错误。感谢大家的宝贵意见!以下是本教程的修改后的最低版本。只需将DynamicMplCanvas用作普通的QWidget。# Copyright (C) 2005 Florent Rougon# 2006 Darren Dale## This file is an example program for matplotlib. It may be used and# modified with no restriction; raw copies as well as modified versions# may be distributed without limitation.from __future__ import unicode_literalsimport sys, os, randomfrom PyQt4 import QtGui, QtCorefrom numpy import arange, sin, pifrom matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvasfrom matplotlib.figure import Figureclass MplCanvas(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) FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) FigureCanvas.updateGeometry(self)class DynamicMplCanvas(MplCanvas): """A canvas that updates itself every second with a new plot.""" def __init__(self, *args, **kwargs): MplCanvas.__init__(self, *args, **kwargs) timer = QtCore.QTimer(self) QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"), self.update_figure) timer.start(1000) def compute_initial_figure(self): self.axes.plot([0, 1, 2, 3], [1, 2, 0, 4], 'r') def update_figure(self): # Build a list of 4 random integers between 0 and 10 (both inclusive) l = [ random.randint(0, 10) for i in range(4) ] self.axes.plot([0, 1, 2, 3], l, 'r') self.draw()