PyQt4 matplotlib不会绘制我的实时数据

时间:2017-02-23 17:27:47

标签: python matplotlib pyqt4

我是PyQt4和实时情节的新手。我试图使用matplotlib和Python2.7在PyQt4应用程序上绘制一些随机实时数据。我的代码如下所示:

#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import random
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys
import time


class mplCanvas(FigureCanvas):

    def __init__(self, parent=None):
        self.fig = plt.figure(1)
        self.ax = self.fig.add_subplot(111)
        self.ax.grid(True)
        self.manager = plt.get_current_fig_manager()
        super(mplCanvas, self).__init__(self.fig)
        self.setParent(parent)
        self.init_figure()


class CustomFigCanvas(mplCanvas):

    def __init__(self, *args, **kwargs):
        mplCanvas.__init__(self, *args, **kwargs)

        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.updateFigure)

    def init_figure(self):

        xaxis = np.arange(0, 100, 1)
        yaxis = np.array([0]*100)
        self.ax.set_title("Realtime Waveform Plot")
        self.ax.set_xlabel("Time")
        self.ax.set_ylabel("Amplitude")
        self.ax.axis([0, 100, -1.5, 1.5])
        self.line1 = self.ax.plot(xaxis, yaxis, '-')
        self.values = []


    def addData(self):

        self.values.append(random.random() * 2 - 1)

    def updateFigure(self):

        self.addData()
        CurrentXAxis=np.arange(len(self.values)-100, len(self.values), 1)
        self.line1[0].set_data(CurrentXAxis, np.array(self.values[-100:]))
        self.ax.axis([CurrentXAxis.min(), CurrentXAxis.max(), -1.5, 1.5])
        self.manager.canvas.draw()

在主应用程序中,我致电graph = CustomFigCanvas() 但它所做的只是用直线打印图表为0,图表根本不更新。我无法弄清楚我做错了什么。为什么我的情节没有更新?我正在尝试几个选项,但仍然有相同的结果。我试图做QThread来发出数据样本,但它仍然无法正常工作。我得到的只是一条直线。你有什么建议吗?任何提示将非常感激。谢谢。

1 个答案:

答案 0 :(得分:1)

在您的代码中,您遇到以下错误:

  • 您从未启动计时器,您应该使用:{your timer}.start({period in ms})
  • 阵列大小不同。
  • self.manager.canvas.draw()更改为self.draw()
  • init_figure从未在mplCanvas
  • 中声明
import sys
from PyQt4 import QtGui

import numpy as np
import matplotlib
matplotlib.use("Qt4Agg")
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import random
from PyQt4 import QtCore


class mplCanvas(FigureCanvas):
    def __init__(self, parent=None):
        self.fig = plt.figure(1)
        self.ax = self.fig.add_subplot(111)
        self.ax.grid(True)
        super(mplCanvas, self).__init__(figure=self.fig)
        self.setParent(parent)
        self.init_figure()

    def init_figure(self):
        pass


class CustomFigCanvas(mplCanvas):
    def __init__(self, *args, **kwargs):
        mplCanvas.__init__(self, *args, **kwargs)
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.updateFigure)
        self.timer.start(100)

    def init_figure(self):
        xaxis = np.arange(0, 100, 1)
        self.values = [0]*100
        yaxis = np.array(self.values)
        self.ax.set_title("Realtime Waveform Plot")
        self.ax.set_xlabel("Time")
        self.ax.set_ylabel("Amplitude")
        self.ax.axis([0, 100, -1.5, 1.5])
        self.line1 = self.ax.plot(xaxis, yaxis, '-')

    def addData(self):
        self.values.append(random.random() * 2 - 1)

    def updateFigure(self):
        self.addData()
        CurrentXAxis = np.arange(len(self.values)-100, len(self.values), 1)
        self.line1[0].set_data(CurrentXAxis, np.array(self.values[-100:]))
        self.ax.axis([CurrentXAxis.min(), CurrentXAxis.max(), -1.5, 1.5])
        self.draw()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    main_widget = QtGui.QWidget()
    l = QtGui.QVBoxLayout(main_widget)
    graph = CustomFigCanvas(main_widget)
    l.addWidget(graph)
    main_widget.show()
    sys.exit(app.exec_())

截图:

enter image description here