【问题标题】:pyqtgraph: add crosshair on mouse_x graph_ypyqtgraph:在 mouse_x graph_y 上添加十字准线
【发布时间】:2019-08-09 04:52:09
【问题描述】:

我想在 mouse_x 位置(工作)之后添加一条垂直线,在曲线之后添加一条水平线(而不是 mouse_y)。在 pyqtgraph 十字准线示例中,它显示了如何在 mouse_x 和 mouse_y 位置之后添加十字准线。但这并没有太大帮助。 以下代码将垂直线位置设置为 mouse_x 位置。但我不知道如何将水平线的位置设置为曲线当前的 y 位置(取决于鼠标 x 位置的位置)。

data1 = 10000 + 15000 * pg.gaussianFilter(np.random.random(size=10000), 10) + 3000 * np.random.random(size=10000)
def mouseMoved(evt):
    pos = evt[0]
    if p1.sceneBoundingRect().contains(pos):
        mousePoint = vb.mapSceneToView(pos)
        index = int(mousePoint.x())
        if index > 0 and index < len(data1):
            label.setText("<span style='font-size: 12pt'>x=%0.1f,   <span style='color: red'>y1=%0.1f</span>,   <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index], data2[index]))

        vLine.setPos(mousePoint.x()) # here i set the vertical line's position to mouse_x position
        #hLinePos = vb.mapToView( vLine.pos() )
        hLine.setPos(data1[mousePoint.x()]) # <-- how do i set this horizontal line so it is kind of snaped to the curve

【问题讨论】:

    标签: python pyqtgraph


    【解决方案1】:

    也许这个十字准线小部件可以为您指明正确的方向

    from PyQt4 import QtCore, QtGui
    import sys
    import numpy as np
    import pyqtgraph as pg
    import random
    
    """Crosshair Plot Widget Example"""
    
    class CrosshairPlotWidget(QtGui.QWidget):
        """Scrolling plot with crosshair"""
    
        def __init__(self, parent=None):
            super(CrosshairPlotWidget, self).__init__(parent)
    
            # Use for time.sleep (s)
            self.FREQUENCY = .025
            # Use for timer.timer (ms)
            self.TIMER_FREQUENCY = self.FREQUENCY * 1000
    
            self.LEFT_X = -10
            self.RIGHT_X = 0
            self.x_axis = np.arange(self.LEFT_X, self.RIGHT_X, self.FREQUENCY)
            self.buffer = int((abs(self.LEFT_X) + abs(self.RIGHT_X))/self.FREQUENCY)
            self.data = []
    
            self.crosshair_plot_widget = pg.PlotWidget()
            self.crosshair_plot_widget.setXRange(self.LEFT_X, self.RIGHT_X)
            self.crosshair_plot_widget.setLabel('left', 'Value')
            self.crosshair_plot_widget.setLabel('bottom', 'Time (s)')
            self.crosshair_color = (196,220,255)
    
            self.crosshair_plot = self.crosshair_plot_widget.plot()
    
            self.layout = QtGui.QGridLayout()
            self.layout.addWidget(self.crosshair_plot_widget)
    
            self.crosshair_plot_widget.plotItem.setAutoVisible(y=True)
            self.vertical_line = pg.InfiniteLine(angle=90)
            self.horizontal_line = pg.InfiniteLine(angle=0, movable=False)
            self.vertical_line.setPen(self.crosshair_color)
            self.horizontal_line.setPen(self.crosshair_color)
            self.crosshair_plot_widget.setAutoVisible(y=True)
            self.crosshair_plot_widget.addItem(self.vertical_line, ignoreBounds=True)
            self.crosshair_plot_widget.addItem(self.horizontal_line, ignoreBounds=True)
    
            self.crosshair_update = pg.SignalProxy(self.crosshair_plot_widget.scene().sigMouseMoved, rateLimit=60, slot=self.update_crosshair)
    
            self.start()
    
        def plot_updater(self):
            """Updates data buffer with data value"""
    
            self.data_point = random.randint(1,101)
            if len(self.data) >= self.buffer:
                del self.data[:1]
            self.data.append(float(self.data_point))
            self.crosshair_plot.setData(self.x_axis[len(self.x_axis) - len(self.data):], self.data)
    
        def update_crosshair(self, event):
            """Paint crosshair on mouse"""
    
            coordinates = event[0]  
            if self.crosshair_plot_widget.sceneBoundingRect().contains(coordinates):
                mouse_point = self.crosshair_plot_widget.plotItem.vb.mapSceneToView(coordinates)
                index = mouse_point.x()
                if index > self.LEFT_X and index <= self.RIGHT_X:
                    self.crosshair_plot_widget.setTitle("<span style='font-size: 12pt'>x=%0.1f,   <span style='color: red'>y=%0.1f</span>" % (mouse_point.x(), mouse_point.y()))
                self.vertical_line.setPos(mouse_point.x())
                self.horizontal_line.setPos(mouse_point.y())
    
        def start(self):
            self.timer = QtCore.QTimer()
            self.timer.timeout.connect(self.plot_updater)
            self.timer.start(self.get_timer_frequency())
    
        def get_crosshair_plot_layout(self):
            return self.layout
    
        def get_timer_frequency(self):
            return self.TIMER_FREQUENCY
    
    if __name__ == '__main__':
        # Create main application window
        app = QtGui.QApplication([])
        app.setStyle(QtGui.QStyleFactory.create("Cleanlooks"))
        mw = QtGui.QMainWindow()
        mw.setWindowTitle('Crosshair Plot Example')
    
        # Create and set widget layout
        # Main widget container
        cw = QtGui.QWidget()
        ml = QtGui.QGridLayout()
        cw.setLayout(ml)
        mw.setCentralWidget(cw)
    
        # Create crosshair plot
        crosshair_plot = CrosshairPlotWidget()
    
        ml.addLayout(crosshair_plot.get_crosshair_plot_layout(),0,0)
    
        mw.show()
    
        ## Start Qt event loop unless running in interactive mode or using pyside.
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()
    

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 1970-01-01
      • 2022-12-23
      • 2021-02-03
      • 1970-01-01
      • 2022-06-23
      • 2013-07-20
      • 2014-11-04
      • 1970-01-01
      相关资源
      最近更新 更多