在 PyQtGraph 中返回鼠标光标坐标

Returning mouse cursor coordinates in PyQtGraph

我是 PyQtGraph 的新手,想用它来快速可视化我的数据采集。以前我使用的是 matplotlib,重绘图形是我的瓶颈。过渡到 PyQtGraph 后,我目前只缺少 matplotlib 的一个功能。即,returning 我的鼠标光标的 x 和 y 坐标。

如何在使用 PyQtGraph 绘制的绘图中 call/mimic 鼠标光标的 x 和 y 坐标 return?

编辑! - 实施 leongold 的提示后,代码能够 return 鼠标光标位置而不会降低速度。代码如下:

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

您需要设置 pyqtgraph.SignalProxy 并将其连接到回调:

如果 p 是您的情节,它将看起来像:pyqtgraph.SignalProxy(p.scene().sigMouseMoved, rateLimit=60, slot=callback)

每当鼠标移到绘图上时,都会使用 event 作为参数调用回调,即 callback(event)event[0] 持有您传递给 p.vb.mapSceneToView(position).x() 的 x 值和 p.vb.mapSceneToView(position).y() 的位置参数。