PyQt5 Pyqtgraph图离散

时间:2018-11-13 15:00:48

标签: python pyqt pyqt5 pyqtgraph

目前,对于这个项目,我正在与matplolib一起追踪我的情节。 不过,我希望在pyqtgraph(Pyqt5)处进行转换。 我没有发现如何用pyqtgraph跟踪FFT的离散图,与图片相同。 你有主意吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以创建一个在其中绘制垂直线的项目:

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

class LinePlot(pg.GraphicsObject):
    def __init__(self, data):
        super(LinePlot, self).__init__()
        self._data = []
        self.setData(data)

    def setData(self, data):
        self._data = data
        self.generate()

    def generate(self):
        self.picture = QtGui.QPicture()
        p = QtGui.QPainter(self.picture)
        p.setPen(pg.mkPen('r', width=2))
        for (t, v) in self._data:
            if v != 0:
                p.drawLine(QtCore.QPointF(t, 0), QtCore.QPointF(t, v))

    def paint(self, p, *args):
        p.drawPicture(0, 0, self.picture)

    def boundingRect(self):
        return QtCore.QRectF(self.picture.boundingRect())

if __name__ == '__main__':
    import sys
    import random

    app = QtGui.QApplication(sys.argv)
    w = QtGui.QMainWindow()
    view = pg.GraphicsLayoutWidget()
    w.setCentralWidget(view)
    plot = view.addPlot()
    vals = [(i, random.randint(0, 255)) for i in range(30)]
    plot.addItem(LinePlot(vals))
    w.show()
    sys.exit(app.exec_())

enter image description here