Попытка получить курсор с отображением координат в виджете графика pyqtgraph в PyQt5

Я пытаюсь добавить вывод позиции курсора в виджет графика pqytplot в PyQt5. Я нашел этот код, который делает то, что я хочу, но в отдельном окне в одном программном файле:

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

#generate layout
app = QtGui.QApplication([])
win = pg.GraphicsWindow()
label = pg.LabelItem(justify='right')
win.addItem(label)
p1 = win.addPlot(row=1, col=0)

data1 = [n**2 for n in range(100)]
p1.plot(data1, pen="r")

#cross hair
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
p1.addItem(vLine, ignoreBounds=True)
p1.addItem(hLine, ignoreBounds=True)


def mouseMoved(evt):
    pos = evt[0]  ## using signal proxy turns original arguments into a tuple
    if p1.sceneBoundingRect().contains(pos):
        mousePoint = p1.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>" % (mousePoint.x(), data1[index]))
        vLine.setPos(mousePoint.x())
        hLine.setPos(mousePoint.y())


proxy = pg.SignalProxy(p1.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Проблема, с которой я сталкиваюсь, заключается в том, чтобы выяснить, как реализовать что-то подобное с моим графическим интерфейсом, где мне нужно будет передать ссылку на plotwidget в функцию mouseMoved. В приведенном выше примере функция mousemoved имеет доступ к hline, vline и p1, но в моем коде этого не будет — мне нужно иметь возможность передать их. Но я понятия не имею, как это сделать.

Я попытался воспроизвести эту проблему с наименьшим возможным объемом кода. Во-первых, вот простой файл пользовательского интерфейса для графического интерфейса, который называется CursorLayout.ui.

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1167</width>
    <height>443</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
     <layout class="QVBoxLayout" name="verticalLayout_6">
      <item>
       <layout class="QHBoxLayout" name="horizontalLayout_16">
        <property name="sizeConstraint">
         <enum>QLayout::SetFixedSize</enum>
        </property>
        <item>
         <layout class="QVBoxLayout" name="verticalLayout">
          <item>
           <layout class="QHBoxLayout" name="horizontalLayout_4">
            <item>
             <widget class="QPushButton" name="startbutton">
              <property name="sizePolicy">
               <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
                <horstretch>0</horstretch>
                <verstretch>0</verstretch>
               </sizepolicy>
              </property>
              <property name="text">
               <string>Plot</string>
              </property>
             </widget>
            </item>
           </layout>
          </item>
         </layout>
        </item>
       </layout>
      </item>
      <item>
       <layout class="QVBoxLayout" name="verticalLayout_5">
        <item>
         <widget class="PlotWidget" name="plotWidget" native="true">
          <property name="sizePolicy">
           <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
           </sizepolicy>
          </property>
          <property name="minimumSize">
           <size>
            <width>0</width>
            <height>300</height>
           </size>
          </property>
         </widget>
        </item>
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout_3"/>
        </item>
        <item>
         <layout class="QHBoxLayout" name="horizontalLayout_17">
          <property name="spacing">
           <number>0</number>
          </property>
          <item>
           <widget class="QPushButton" name="exitbutton">
            <property name="sizePolicy">
             <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
              <horstretch>0</horstretch>
              <verstretch>0</verstretch>
             </sizepolicy>
            </property>
            <property name="text">
             <string>Exit</string>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
 </widget>
 <customwidgets>
  <customwidget>
   <class>PlotWidget</class>
   <extends>QWidget</extends>
   <header location="global">pyqtgraph</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

Основная программа такова:

from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMainWindow

from initGUI import connecttolayout, setinitialview


class UI(QMainWindow):
    def __init__(self):
        super(UI, self).__init__()
        uic.loadUi("CursorLayout.ui", self)  #load GUI layout file created with QtDesigner
        connecttolayout(self)  # connect code to elements in UI file
        setinitialview(self)  # set initial view (button/label visibility, default values, etc)
        self.show()


    def clickedstartButton(self):  #action if start button clicked
        self.plotWidget.clear()
        plotx = range(100)
        ploty = [number**2 for number in plotx]
        thisline = self.plotWidget.plot(plotx, ploty, pen='r')
        QApplication.processEvents()


    def clickedexitButton(self):
        self.close()


app=QApplication([])
UIWindow=UI()
app.exec()

с файлом, содержащим код для настройки графического интерфейса, initGUI.py (не обязательно, как вы это сделаете, но это должно имитировать файловую структуру моей более крупной программы):

from PyQt5.QtWidgets import QPushButton
import pyqtgraph as pg

def connecttolayout(self):  #connect GUI elements to elements in UI file
    self.startButton = self.findChild(QPushButton, "startbutton")
    self.exitButton = self.findChild(QPushButton, "exitbutton")
    self.startButton.clicked.connect(self.clickedstartButton)
    self.exitButton.clicked.connect(self.clickedexitButton)

def mouseMoved(evt):
    pos = evt[0]  ## using signal proxy turns original arguments into a tuple
    if self.plotWidget.sceneBoundingRect().contains(pos):
        mousePoint = self.plotWidget.vb.mapSceneToView(pos)
        index = int(mousePoint.x())
        #if index > 0 and index < len(data1):
        if index > 0 and index < self.MFmax:
            self.cursorlabel.setText("<span style='font-size: 12pt'>x=%0.1f,   <span style='color: red'>y=%0.1f</span>" % (
            mousePoint.x(), mousePoint.y()))
        self.vLine.setPos(mousePoint.x())
        self.hLine.setPos(mousePoint.y())


def setinitialview(self): #set initial view to pvst view and clear plot window
    #set plot initial configuration
    self.plotWidget.setBackground('w')
    self.plotWidget.setLabels(left=('Pressure', 'Torr'))
    self.plotWidget.setLabel('left',color='black',size=30)
    self.plotWidget.setLabels(bottom=('Time', 's'))
    self.plotWidget.setLabel('bottom',color='black',size=30)
    self.plotWidget.clear()

    # cross hair
    self.vLine = pg.InfiniteLine(angle=90, movable=False)
    self.hLine = pg.InfiniteLine(angle=0, movable=False)
    self.plotWidget.addItem(self.vLine, ignoreBounds=True)
    self.plotWidget.addItem(self.hLine, ignoreBounds=True)
    self.cursorlabel = pg.LabelItem(justify='right')
    proxy = pg.SignalProxy(self.plotWidget.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)

Я на самом деле удивлен, что моя попытка не вызывает ошибку - нажатие кнопки графика создает график, но определенно не создает курсор на графике в графическом интерфейсе.

Как передать необходимую информацию функции mouseMoved?


person Matthew Lawrence    schedule 31.07.2020    source источник


Ответы (1)


Есть несколько небольших ошибок, которые могут привести к сбою вашей программы:

  • Функция mouseMoved() должна быть внутри вашего класса виджета, потому что ей нужен аргумент evt, который генерируется в виджете.

  • Переменная/константа self.MFmax нигде не создается

  • В этой строке:

    mousePoint = self.plotWidget.vb.mapSceneToView(pos)
    

    Объект PlotWidget не имеет атрибута vb. Это атрибут PlotItem, тогда вы должны изменить эту строку на эту:

    mousePoint = self.plotWidget.plotItem.vb.mapSceneToView(pos)
    
  • Pyqtgraph рекомендует здесь использовать TextItem вместо LabelItem , чтобы отобразить текст внутри масштабированного представления из-за его масштабируемого размера.

Теперь, с учетом сказанного и реорганизации вашего кода, чтобы сделать его более разборчивым, вот мое решение для вашего кода (вам нужен только файл UI и этот скрипт):

import sys
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, uic

ui_file = uic.loadUiType("CursorLayout.ui")[0]

class UI(QtGui.QMainWindow, ui_file):
    def __init__(self):
        ## Inherit the QMainWindow and ui_file classes
        QtGui.QMainWindow.__init__(self)
        ui_file.__init__(self)
        self.setupUi(self)
        ## Create aditional widgets
        self.plot_item = self.plotWidget.plot()
        self.vLine = pg.InfiniteLine(angle=90, movable=False)
        self.hLine = pg.InfiniteLine(angle=0, movable=False)
        self.cursorlabel = pg.TextItem(anchor=(-1,10))
        ## Build the rest of the GUI
        self.format_plot()
        ## data
        self.plotx = range(100)
        self.ploty = [number**2 for number in self.plotx]        
        ## Connect signals to actions
        self.startbutton.clicked.connect(self.clickedstartButton)
        self.exitbutton.clicked.connect(self.clickedexitButton)
        self.plotWidget.scene().sigMouseMoved.connect(self.mouseMoved)
    
    ## OVERWRITE the mouseMoved action:
    def mouseMoved(self, evt):
        pos = evt
        if self.plotWidget.sceneBoundingRect().contains(pos):
            mousePoint = self.plotWidget.plotItem.vb.mapSceneToView(pos)
            index = int(mousePoint.x())
            if index > 0 and index < len(self.plotx):
            # if index > 0 and index < self.MFmax:
                self.cursorlabel.setHtml(
                    "<span style='font-size: 12pt'>x={:0.1f}, \
                     <span style='color: red'>y={:0.1f}</span>".format(
                mousePoint.x(), mousePoint.y()))
            self.vLine.setPos(mousePoint.x())
            self.hLine.setPos(mousePoint.y())
  
    def clickedstartButton(self):  #action if start button clicked
        self.plot_item.setData(self.plotx, self.ploty, pen='r')
        self.plotWidget.addItem(self.cursorlabel)

    def clickedexitButton(self):
        self.close()
    
    def format_plot(self):
        self.plotWidget.setBackground('w')
        self.plotWidget.setLabels(left=('Pressure', 'Torr'))
        self.plotWidget.setLabel('left',color='black',size=30)
        self.plotWidget.setLabels(bottom=('Time', 's'))
        self.plotWidget.setLabel('bottom',color='black',size=30)
        self.plotWidget.addItem(self.vLine, ignoreBounds=True)
        self.plotWidget.addItem(self.hLine, ignoreBounds=True)
        
    
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = UI()
    window.show()
    sys.exit(app.exec_())

Приведенный выше код заставит перекрестие (hline и vline) следовать за вашей мышью и отобразит координаты этой позиции, например:

введите здесь описание изображения

Если вы хотите, чтобы перекрестие отслеживало точки кривой на основе положения курсора по оси X, вы можете изменить функцию mouseMoved() на это:

def mouseMoved(self, evt):
        pos = evt
        if self.plotWidget.sceneBoundingRect().contains(pos):
            mousePoint = self.plotWidget.plotItem.vb.mapSceneToView(pos)
            mx = np.array([abs(i-mousePoint.x()) for i in self.plotx])
            index = mx.argmin()
            if index >= 0 and index < len(self.plotx):
                self.cursorlabel.setHtml(
                    "<span style='font-size: 12pt'>x={:0.1f}, \
                     <span style='color: red'>y={:0.1f}</span>".format(
                     self.plotx[index], self.ploty[index])
                     )
            self.vLine.setPos(self.plotx[index])
            self.hLine.setPos(self.ploty[index])

И это будет результат:

введите здесь описание изображения

person Alejandro Condori    schedule 30.12.2020