pyqtgraph:在鼠标\u x图形\u y上添加十字线

2024-06-12 14:22:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在鼠标x位置后添加一条垂直线(工作),在曲线后添加一条水平线(而不是鼠标y)。在pyqtgraph十字线示例中,它显示了如何在鼠标\ux和鼠标\uy位置之后添加十字线。但这并没有多大帮助。 下面的代码将垂直线位置设置为鼠标位置。但是我不知道如何将水平线的位置设置为曲线当前的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

Tags: possizeindexifstylenprandom鼠标
1条回答
网友
1楼 · 发布于 2024-06-12 14:22:15

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

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_()

相关问题 更多 >