如何在pyqtgraph中绘制十字准线并绘制鼠标位置? [英] How to draw crosshair and plot mouse position in pyqtgraph?

查看:115
本文介绍了如何在pyqtgraph中绘制十字准线并绘制鼠标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 和 pyqtgraph 的新手.我正在研究不同类型信号的查看器.目前,当我想在鼠标位置中包含一个十字准线和一个文本标签时,我被卡住了.我正在使用 GridLayout,因为后来图表与其他几个元素结合在一起.

I am new to Python and pyqtgraph. I am working on a viewer for different kinds of signals. Currrently I got stuck when I want to include a crosshair and a text label with the mouse position. I am working with GridLayout, because later the graph is combined with several other elements.

我尝试改编 pyqtgraph 示例 Crosshair/mouse interaction,但除了 pyqtgraph 中的许多其他内容之外,我不明白在 mousemoved() 和脚本之前 vb = signalgraph.vb 的含义引发 NameError

I tried to adapt the pyqtgraph example Crosshair / mouse interaction but apart from many other things in pyqtgraph I do not understand the meaning of vb = signalgraph.vb just before mousemoved() and it the script raises a NameError

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

#QtGui.QApplication.setGraphicsSystem('raster')
app = QtGui.QApplication([])
mainwindow = QtGui.QMainWindow()
mainwindow.setWindowTitle('pyqtgraph example: PlotWidget')
mainwindow.resize(1000,800)
cw = QtGui.QWidget()
mainwindow.setCentralWidget(cw)

gridlayout = QtGui.QGridLayout()
cw.setLayout(gridlayout)

# define plot windows
signalgraph = pg.PlotWidget(name='Signalgraph')

# set position and size of plot windows
gridlayout.addWidget(signalgraph,0,0)

mainwindow.show()


# sample data
x = [0,1,2,3,4,5,6,7,8,9,10]
y = [0,0,0,8,8,8,9,9,9,0,0]

# plot 1
curve = pg.PlotCurveItem(x,y[:-1],pen='w',stepMode=True)
signalgraph.addItem(curve)

#cross hair in signalgraph
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
signalgraph.addItem(vLine, ignoreBounds=True)
signalgraph.addItem(hLine, ignoreBounds=True)

# Here I am not sure what to do ...
vb = signalgraph.vb
##vb = pg.ViewBox()


def mouseMoved(evt):
    pos = evt[0]
    if signalgraph.sceneBoundingRect().contains(pos):
        mousePoint = vb.mapSceneToView(pos)
        index = int(mousePoint.x())
        if index > 0 and index < len(x):
            label.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>" % (mousePoint.x(), y[index], data2[index]))
        vLine.setPos(mousePoint.x())
        hLine.setPos(mousePoint.y())


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


# Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

非常感谢问候迈克尔

推荐答案

"vb" 是 PlotItem 类的一个属性.由于 signalgraph 是一个 PlotWidget,您可能不希望它具有相同的属性(尽管它确实包装了其内部 PlotItem 中的一些方法).所以你需要的代码是:

"vb" is an attribute of the PlotItem class. Since signalgraph is a PlotWidget, you might not expect it to have the same attributes (although it does wrap some methods from its internal PlotItem). So the code you need is:

vb = signalgraph.plotItem.vb

如果您对 PlotWidget 和 PlotItem 之间的区别感到困惑,请阅读 QGraphicsView 和 QGraphicsItem 类(均在 Qt 文档中).PlotWidget 只不过是一个 QGraphicsView,里面显示了一个 PlotItem.

If you are confused about the difference between PlotWidget and PlotItem, read up on the QGraphicsView and QGraphicsItem classes (both in the Qt documentation). PlotWidget is little more than a QGraphicsView with a PlotItem displayed inside.

这篇关于如何在pyqtgraph中绘制十字准线并绘制鼠标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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