【问题标题】:Returning mouse cursor coordinates in PyQtGraph在 PyQtGraph 中返回鼠标光标坐标
【发布时间】:2016-06-02 09:03:38
【问题描述】:

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

如何在使用 PyQtGraph 制作的绘图中调用/模拟鼠标光标的 x 和 y 坐标的返回?

编辑! - 实现leongold的提示后,代码可以在不失速的情况下返回鼠标光标位置。代码如下:

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

【问题讨论】:

    标签: python plot pyqt pyqtgraph


    【解决方案1】:

    您需要设置一个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() 用于 y 值。

    【讨论】:

      猜你喜欢
      • 2021-02-10
      • 2017-06-01
      • 2021-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-20
      相关资源
      最近更新 更多