在PyQtGraph中返回鼠标光标坐标 [英] Returning mouse cursor coordinates in PyQtGraph

查看:1427
本文介绍了在PyQtGraph中返回鼠标光标坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PyQtGraph的新手,并希望将其用于快速可视化我的数据采集.以前,我使用matplotlib来重画图形是我的瓶颈.过渡到PyQtGraph之后,我目前仅缺少matplotlib的一项功能.即,返回鼠标光标的x坐标和y坐标.

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 transitioning to PyQtGraph, I am currently missing only one functionality of matplotlib. Namely, returning the x-, and y-coordinate of my mouse cursor.

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

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

编辑! -实施leongold技巧后,代码可以返回mousecursor位置,而不会损失速度.代码如下:

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并将其连接到回调:

You need to setup a pyqtgraph.SignalProxy and connect it to a callback:

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

if p is your plot, it'll look like: 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值.

Whenever the mouse is moved over the plot, the callback is called with an event as an argument, i.e. callback(event). event[0] holds a positional argument you pass to p.vb.mapSceneToView(position).x() for x value and p.vb.mapSceneToView(position).y() for y value.

这篇关于在PyQtGraph中返回鼠标光标坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆