向pyqtgraph动态图添加标记

时间:2019-01-04 18:16:43

标签: python-2.7 pyqtgraph

我正在用python做频谱分析仪。现在,我在向光谱图中添加标记时遇到了问题。我不知道该怎么做,也找不到一个例子。请告诉我。先感谢您 这是我的代码的一部分,在其中创建图形并显示它们。每个图都是单独的流。

enter image description here

class Test(QtGui.QDockWidget):

def __init__(self, nember, band, number_thread, bandw):
    super(Test, self).__init__()
    self.setAcceptDrops(True)

    self.bandw = bandw
    self.dockedWidget = QtGui.QWidget()
    self.setAcceptDrops(True)
    self.number_thread = number_thread
    self.image_item = pg.ImageItem()
    pos = np.array([0., 1., 0.5, 0.25, 0.75])
    color = np.array([[0, 0, 0, 0], [255, 255, 0, 255], [0, 0, 0, 255], (0, 0, 255, 255), (255, 0, 0, 255)],
                     dtype=np.ubyte)
    cmap = pg.ColorMap(pos, color)
    lut = cmap.getLookupTable(0.0, 1.0, 256)
    self.image_item.setLookupTable(lut)
    self.image_item.setLevels([-5, 16])


    self.win = pg.PlotWidget()
    self.win1 = pg.PlotWidget()
    self.hLine=pg.InfiniteLine(pos=0, angle=0, movable=True, pen='r')
    self.win.addItem(self.hLine)
    self.hLine.addMarker(',')
    self.win1.addItem(self.image_item)
    self.win.setLabel('left', 'Амплитуда'.decode('utf-8'), units='dB')
    self.win.setLabel('bottom', 'Частота'.decode('utf-8'), units='Hz')
    self.plot_data = self.win.plot()

    self.plot_maxhold = self.win.plot()
    self.win.setLimits(xMin=min(band), xMax=max(band))
    self.checkbut_maxhold = QtGui.QCheckBox('МАКСХОЛД'.decode('utf-8'))
    self.checkbutton = QtGui.QCheckBox('ВАША РЕКЛАМА1'.decode('utf-8'))
    self.checkbutt2 = QtGui.QCheckBox('ВАША РЕКЛАМА2'.decode('utf-8'))
    self.nember = nember
    button_layout = QtGui.QHBoxLayout()
    button_layout.addWidget(self.checkbut_maxhold)
    button_layout.addWidget(self.checkbutton)
    button_layout.addWidget(self.checkbutt2)

    layout = QtGui.QVBoxLayout()
    layout.addItem(button_layout)
    layout.addWidget(self.win)
    layout.addWidget(self.win1)
    #layout.addLayout(self.image_widget)
    # self.dockedWidget.addWidget(self.win)

    self.dockedWidget.setLayout(layout)
    self.setWidget(self.dockedWidget)
    self.thread = Tread_400(self.plot_data,self.image_item, self.plot_maxhold, self.nember, band)
    self.thread.start()
    self.checkbut_maxhold.stateChanged.connect(self.state_changed_button1)

def closeEvent(self, QCloseEvent):
    data.list_band.remove(data.list_band[data.list_freq.index(self.bandw)])
    data.list_freq.remove(self.bandw)

    print (self.nember)
    self.thread.stop()

    del data.shared_data[self.nember]
    print(data.list_band)
    print(data.list_freq)

def set_value(self):
    print(1)
    self.range_changed.emit(22)

def tested(self, vall):
    print (vall)

def state_changed_button1(self, int):
    if self.checkbut_maxhold.isChecked():
        self.thread.flag_maxhold = True
        print("CHECKED!")

    else:
        print("UNCHECKED!")

        self.thread.flag_maxhold = False

def state_changed_button2(self, int):
    if self.checkbut_maxhold.isChecked():
        self.thread.reklama1 = True
        print("CHECKED!")

    else:
        print("UNCHECKED!")

        self.thread.reklama1 = False

def state_changed_button3(self, int):
    if self.checkbut_maxhold.isChecked():
        self.thread.reklama2 = True
        print("CHECKED!")

    else:
        print("UNCHECKED!")

        self.thread.reklama2 = False

class Tread_400(QtCore.QThread):
    def __init__(self, wind,plot_waterfall, plot_maxhold, number, band):
        QtCore.QThread.__init__(self)
        print(self.currentThreadId())
        self.flag_maxhold = None
        self.reklama1 = True
        self.reklama2 = False
        self.x_axis = band
        self.img_array = np.zeros((100, len(self.x_axis)))
        self.plot_maxhold = plot_maxhold
        self.wind = wind
        self.plot_waterfall=plot_waterfall
        self.number = number
        self.timer1 = QtCore.QTimer()
        print(self.timer1.timerId())
        self.maxhold22 = np.ones(data.shared_data[self.number].size) * -1000
        print(self.number)
        self.timer1.timeout.connect(self.plot_data)

    def run(self):

        self.timer1.start(0.0001)

    def plot_data(self):

        self.wind.setData(self.x_axis, data.shared_data[self.number], clear=True)
        if self.flag_maxhold:
            maxhhoold = self.maxhold(data.shared_data[self.number], self.maxhold22)
            self.plot_maxhold.setData(self.x_axis, maxhhoold[:], clear=True, pen='r')
        elif self.flag_maxhold == False:
            self.plot_maxhold.clear()
            self.maxhold22 = np.ones(data.shared_data[self.number].size) * -1000
            self.maxhold22=None
        if self.reklama1==True:
            start=time.time()
            self.img_array = np.roll(self.img_array, -1, 0)
            self.img_array[-1:] = data.shared_data[self.number]
            self.plot_waterfall.setImage(self.img_array.T, autoLevels=False)
            print (time.time()-start)
    def stop(self):
        self.timer1.stop()
        self.terminate()
        self.wait()


    def maxhold(self, data, maxhold):
        for i in np.arange(data.size):
            if data[i] > maxhold[i]:
                maxhold[i] = data[i]
        return maxhold

1 个答案:

答案 0 :(得分:0)

似乎您可以在有或没有a TextItem(或an ArrowItem)的情况下使用CurveArrow来使自己接近想要的东西?

请参见TextItem的示例文件:pyqtgraph/examples/text.py

enter image description here