Python Pyqtgraph中的十字线类
我想定义一个名为Crosshair的类,它可以附加到pyqtgraph中的绘图。我在示例中发现了以下代码片段:Python Pyqtgraph中的十字线类,python,pyqtgraph,Python,Pyqtgraph,我想定义一个名为Crosshair的类,它可以附加到pyqtgraph中的绘图。我在示例中发现了以下代码片段: #cross hair vLine = pg.InfiniteLine(angle=90, movable=False) hLine = pg.InfiniteLine(angle=0, movable=False) p1.addItem(vLine, ignoreBounds=True) p1.addItem(hLine, ignoreBounds=True) vb = p1.v
#cross hair
vLine = pg.InfiniteLine(angle=90, movable=False)
hLine = pg.InfiniteLine(angle=0, movable=False)
p1.addItem(vLine, ignoreBounds=True)
p1.addItem(hLine, ignoreBounds=True)
vb = p1.vb
def mouseMoved(evt):
pos = evt[0] ## using signal proxy turns original arguments into a tuple
if p1.sceneBoundingRect().contains(pos):
mousePoint = vb.mapSceneToView(pos)
index = int(mousePoint.x())
if index > 0 and index < len(data1):
label.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>, <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index], data2[index]))
vLine.setPos(mousePoint.x())
hLine.setPos(mousePoint.y())
proxy = pg.SignalProxy(p1.scene().sigMouseMoved, rateLimit=60, slot=mouseMoved)
#十字线
vLine=pg.InfiniteLine(角度=90,可移动=False)
hLine=pg.InfiniteLine(角度=0,可移动=False)
p1.附加项(vLine,ignoreBounds=True)
p1.添加项(hLine,ignoreBounds=True)
vb=p1.vb
def鼠标移动(evt):
pos=evt[0]##使用信号代理将原始参数转换为元组
如果p1.SceneBondingDirect()包含(位置):
mousePoint=vb.MapScenetView(pos)
index=int(mousePoint.x())
如果索引>0且索引
我开始将其转换为一个类,如下所示:
class CrossHair():
def __init__(self, p1):
self.vLine = pg.InfiniteLine(angle=90, movable=False)
self.hLine = pg.InfiniteLine(angle=0, movable=False)
self.p1 = p1
self.vb = self.p1.vb
p1.addItem(self.vLine, ignoreBounds=True)
p1.addItem(self.hLine, ignoreBounds=True)
self.proxy = pg.SignalProxy(self.p1.scene().sigMouseMoved, rateLimit=60, slot=self.mouseMoved)
def mouseMoved(self, evt):
pos = evt[0] ## using signal proxy turns original arguments into a tuple
if self.p1.sceneBoundingRect().contains(pos):
mousePoint = self.vb.mapSceneToView(pos)
index = int(mousePoint.x())
# if index > 0 and index < len(data1):
# label.setText("<span style='font-size: 12pt'>x=%0.1f, <span style='color: red'>y1=%0.1f</span>, <span style='color: green'>y2=%0.1f</span>" % (mousePoint.x(), data1[index], data2[index]))
self.vLine.setPos(mousePoint.x())
self.hLine.setPos(mousePoint.y())
ch = CrossHair(p1)
class CrossHair():
定义初始(自我,p1):
self.vLine=pg.InfiniteLine(角度=90,可移动=False)
self.hLine=pg.InfiniteLine(角度=0,可移动=False)
self.p1=p1
self.vb=self.p1.vb
p1.附加项(self.vLine,ignoreBounds=True)
p1.添加项(self.hLine,ignoreBounds=True)
self.proxy=pg.SignalProxy(self.p1.scene().sigMouseMoved,rateLimit=60,slot=self.mouseMoved)
def mouseMoved(self,evt):
pos=evt[0]##使用信号代理将原始参数转换为元组
如果self.p1.sceneboundingdirect()包含(pos):
mousePoint=self.vb.MapScenetView(pos)
index=int(mousePoint.x())
#如果索引>0且索引
这是正确的方法吗?
换句话说,我将情节附加到十字准线的做法是否正确?我本想做相反的事,但我不知道怎么做,也不知道这样做是否正确
另外,如何从绘图本身检索数据值(注释部分)?更好的方法是创建pg.GraphicsObject的子类,其中包含两条无穷线作为子类。参考资料:,另请参见customGraphicsItem示例 该类应该有一个setPos()方法,用于设置十字线原点的位置。然后,您可以添加一些应用程序级代码来跟踪鼠标位置并相应地更新十字线,如十字线示例所示。或者,您可以让十字线本身自动跟踪鼠标位置 关于第二个问题:您至少需要告诉十字线它应该查询哪些PlotDataItem或PlotCurveItem,以确定与垂直线相交的y位置