Returning mouse cursor coordinates in PyQtGraph

2,769 views
Skip to first unread message

Sebastiaan Greveling

unread,
Feb 20, 2016, 2:58:28 PM2/20/16
to pyqtgraph

I am new to PyQtGraph and want to use it for a speedy visualization of my data acquisition. Previously I was using matplotlib where redrawing the figure was my bottleneck. After making the transition to PyQtGraph, I am currently missing only one functionality of matplotlib. Namely, returning the x-, and y-coordinate of my mouse cursor.


How can I call/mimic the return of the x-, and y-coordinates of my mouse cursor in a plot made using PyQtGraph?


I have had a look at the HoverEvent documentation. However, with my code I am not able to figure out how to call these functions. All the errors I produce tell me that the attribute I am trying to call is not contained by the function I am using.

A simple version of the code I will be using for my data acquisition is found below:


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

x = numpy.linspace(-2 * numpy.pi, 2 * numpy.pi, 1000)
y = numpy.cos(x)

# Plot
win = pg.GraphicsWindow()
win.resize(800, 800)

p = win.addPlot()
p.plot(x, y, pen = "y")

i = 0
while i < 350:
  noise = numpy.random.normal(0, 1, len(y))
  y_new = y + noise

  p.plot(x, y_new, pen = "y", clear = True)
  p.enableAutoRange("xy", False)

  pg.QtGui.QApplication.processEvents()

  i += 1
win.close()

shaera...@gmail.com

unread,
Feb 25, 2016, 4:03:42 PM2/25/16
to pyqtgraph

I did it this way: override the mouseMoveEvent().. I dont no if there is a better way to do this .. 

class  graphicsLayoutWidget(pg.GraphicsLayoutWidget):
    def __init__(self):
        pg.GraphicsLayoutWidget.__init__(self)
        
    def mouseMoveEvent(self, event):
        print event.pos()

app = QtGui.QApplication([])
x = numpy.linspace(-2 * numpy.pi, 2 * numpy.pi, 1000)
y = numpy.cos(x)

# Plot
win = graphicsLayoutWidget()
win.show()
win.resize(800, 800)

p = win.addPlot()
p.plot(x, y, pen = "y")

i = 0
while 1:
  noise = numpy.random.normal(0, 1, len(y))
  y_new = y + noise

  p.plot(x, y_new, pen = "y", clear = True)
  p.enableAutoRange("xy", False)

  i += 1
  pg.QtGui.QApplication.processEvents()
win.close()

Vincent Le Saux

unread,
Feb 26, 2016, 12:34:02 AM2/26/16
to pyqtgraph
Hi,

I think you should have a look at the crosshair.py example. Another (and better to my opinion) solution is proposed.

Vincent

Sebastiaan Greveling

unread,
Feb 26, 2016, 10:15:19 AM2/26/16
to pyqt...@googlegroups.com
Thank you very much Vincent. Until your remark I only had a very quick look at the examples. With the crosshair.py example I am now able to return the mouse position without loosing plotting speed. The piece of code I have now is the following:



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


def gaussian(A, B, x):
  return A * numpy.exp(-(x/(2. * B))**2.)

def mouseMoved(evt):
  mousePoint = p.vb.mapSceneToView(evt[0])
  label.setText("<span style='font-size: 14pt; color: white'> x = %0.2f, <span style='color: white'> y = %0.2f</span>" % (mousePoint.x(), mousePoint.y()))

# Initial data frame
x = numpy.linspace(-5., 5., 10000)
y = gaussian(5., 0.2, x)

# Generate layout
win = pg.GraphicsWindow()
label = pg.LabelItem(justify = "right")
win.addItem(label)

p = win.addPlot(row = 1, col = 0)

plot = p.plot(x, y, pen = "y")

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

# Update layout with new data
i = 0
while i < 500:
  noise = numpy.random.normal(0, .2, len(y))

  y_new = y + noise

  plot.setData(x, y_new, pen = "y", clear = True)

  p.enableAutoRange("xy", False)

  pg.QtGui.QApplication.processEvents()

  i += 1
win.close()

--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/ZlxPGqHCZJ0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/479e2826-1a42-45f6-9efd-a10406f0276f%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Vincent Le Saux

unread,
Feb 26, 2016, 1:07:33 PM2/26/16
to pyqtgraph
Thanks for sharing your piece of code. It could be useful for other users!

Vincent
Reply all
Reply to author
Forward
0 new messages