I am plotting some data points using ScatterPlotItems(). I am trying to show a tooltip that should appear when the mouse hovers over a datapoint.Can anybody help me with this?
class Tooltip_example:
def __init__(self,x,y,widget):
self.scatterPoints = pg.ScatterPlotItem(x,y,size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120))
self.display_text= pg.TextItem(text='',color=(176,23,31),anchor=(1,1))
self.display_text.hide()
widget.addItem(self.scatterPoints)
widget.addItem(self.display_text)
self.scatterPoints.scene().sigMouseMoved.connect(self.onMove)
def onMove(self,pos):
act_pos = self.scatterPoints.mapFromScene(pos)
p1 = self.scatterPoints.pointsAt(act_pos)
if len(p1)!=0:
self.display_text.setText('x=%f\nY=%f'%(x[i],y[i]))
self.display_text.setPos(x[i],y[i])
self.display_text.show()
else:
display_text.hide()
I am new to python so, please excuse me for any stupid mistake that I might have made in my code.
Hi Luke,I followed your strategy and I could sucessfully implement the tooltip feature that I was looking for.However, when I try to implement the solution using classes it doesn't work. Following is the code that I have written:
When I create an instance of Tooltip_example class and and do not assign it to any variable on the L.H.S the control is never passed to the 'onMove' event.Tooltip_example(x, y, widget1) # doesn't workt = Tooltip_example(x, y, widget1) # works fineAny reason for the above mentioned behavior? Or have I missed something?
4) Use a TextItem to display your tooltip