Get data point where mouse is

104 views
Skip to first unread message

Bobby Lucero

unread,
Apr 4, 2019, 11:14:18 AM4/4/19
to pyqtgraph

dashboard.png

The image above is an example of what i'd like to do

Hello, i'd like to get the data at a certain point where my mouse is hovered over the graph. I'm trying to get the circle to hover over the data point where the mouse is, as demonstrated in the screenshot. I already have code to track the mouses position, I just have no idea to get the actual plot data where the mouse is.

Any help would be greatly appreciated.

Patrick

unread,
Apr 4, 2019, 11:10:04 PM4/4/19
to pyqtgraph
Hi,

Still needs a bit of polish, but how about:

from PyQt5 import QtWidgets
import pyqtgraph as pg
import numpy as np

class TestPlot(pg.GraphicsLayoutWidget):

   
def __init__(self):
       
super().__init__()
       
self.curveData = np.random.rand(200)
       
self.plotItem = self.addPlot()
       
self.plotDataItem = self.plotItem.plot(self.curveData)
       
self.plotHighlight = pg.ScatterPlotItem(size=10, pen={"color": "#8080ff"}, brush="#000000")
       
self.plotItem.addItem(self.plotHighlight, ignoreBounds=True)
       
self.plotLabel = pg.TextItem("X", anchor=(0.5, 1.0))
       
self.plotItem.addItem(self.plotLabel, ignoreBounds=True)

       
self.selected_x_i = 0
       
# Create the infinite line to indicate an x coordinate
       
self.crosshairx = pg.InfiniteLine(angle=90, movable=False, pen={"color": "#8080ff"})
       
self.plotItem.addItem(self.crosshairx, ignoreBounds=True)

       
self.signalproxy = pg.SignalProxy(self.plotItem.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)

   
def mouseMoved(self, event):
        pos
= event[0]  ## using signal proxy turns original arguments into a tuple
       
if self.plotItem.sceneBoundingRect().contains(pos):
            mousePoint
= self.plotItem.getViewBox().mapSceneToView(pos)
            index
= int(mousePoint.x())
           
if (index > 0 and index < self.curveData.shape[0] and
                mousePoint
.y() >= self.plotItem.getViewBox().viewRange()[1][0] and
                mousePoint
.y() <= self.plotItem.getViewBox().viewRange()[1][1]):
               
#self.crosshairx.label.setFormat("{:0.2f}".format(self.curveData[index]))s
               
self.crosshairx.setPos(mousePoint.x())
               
self.plotHighlight.setData([index], [self.curveData[index]])
               
self.plotLabel.setPos(index, self.curveData[index])
               
self.plotLabel.setText("{:0.2g}".format(self.curveData[index]))
           
else:
               
# Could proably hide the crosshair in better way...
               
self.crosshairx.setPos(self.plotItem.getViewBox().viewRange()[0][0] - 1)
               
self.plotHighlight.clear()
               
self.plotLabel.setText("")

def main():
   
import sys
    app
= QtWidgets.QApplication(sys.argv)
    mainwindow
= TestPlot()
    mainwindow
.show()
    sys
.exit(app.exec_())


if __name__ == '__main__':
    main
()


Patrick

Bobby Elliott

unread,
Apr 6, 2019, 1:03:55 PM4/6/19
to pyqt...@googlegroups.com

That’s pretty much exactly what I wanted, thank you!

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/deddfe62-8d22-4d7e-ac5e-f4ac7b364d48%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

Reply all
Reply to author
Forward
0 new messages