setting tooltip in pyqtgraph

3,541 views
Skip to first unread message

pyqtuser123

unread,
Dec 20, 2013, 12:25:52 AM12/20/13
to pyqt...@googlegroups.com
Hi,

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?

Luke Campagnola

unread,
Dec 21, 2013, 3:39:08 PM12/21/13
to pyqt...@googlegroups.com
On Fri, Dec 20, 2013 at 12:25 AM, pyqtuser123 <pankaj.k...@gmail.com> wrote:
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?

I recommend the following strategy:
1) Connect to GraphicsScene.sigMouseMoved to get updates on mouse position
2) Map the scene coordinates of the mouse cursor to the local coordinates of the ScatterPlotItem
3) Use ScatterPlotItem.pointsAt(pos) to determine which point(s) are under the cursor
4) Use a TextItem to display your tooltip

Give this a try and let me know if you need further help.

Luke


Message has been deleted
Message has been deleted

pyqtuser123

unread,
Dec 23, 2013, 5:47:12 AM12/23/13
to pyqt...@googlegroups.com
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:

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.                           

Luke Campagnola

unread,
Dec 23, 2013, 8:28:56 AM12/23/13
to pyqt...@googlegroups.com
On Mon, Dec 23, 2013 at 5:47 AM, pyqtuser123 <pankaj.k...@gmail.com> wrote:
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:

Your code looks good to me; this is very close to the way I would have implemented it. Can you explain what part of your solution does not work?

Mike

unread,
Dec 23, 2013, 11:30:36 PM12/23/13
to pyqt...@googlegroups.com

Looks like you have an error in your class. x and y are passed in to __init__, but are also used in OnMove even though they are out of scope. If you want to use them in OnMove, you will need to add

self.x = x
self.y = y

and then replace x and y in OnMove with self.x and self.y.

Mike

pyqtuser123

unread,
Dec 24, 2013, 12:51:31 AM12/24/13
to pyqt...@googlegroups.com
Thanks Mike. It works now.:)

However, there's one more thing that I have noticed. 

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 work
             t = Tooltip_example(x, y, widget1)       # works fine

Any reason for the above mentioned behavior? Or have I missed something?

Luke Campagnola

unread,
Dec 24, 2013, 1:35:13 AM12/24/13
to pyqt...@googlegroups.com
On Tue, Dec 24, 2013 at 12:51 AM, pyqtuser123 <pankaj.k...@gmail.com> wrote:
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 work
             t = Tooltip_example(x, y, widget1)       # works fine

Any reason for the above mentioned behavior? Or have I missed something?

Most likely, the instance is being garbage collected because there are no references to it. One might think that adding the item to a scene should increment its reference count, but I guess the PyQt / PySide developers decided against that for some reason.
 

Kernc

unread,
Jul 22, 2015, 1:35:48 PM7/22/15
to pyqtgraph
On Saturday, December 21, 2013 at 9:39:08 PM UTC+1, Luke Campagnola wrote:

4) Use a TextItem to display your tooltip

Could it instead make sense to call Qt's

    scatterplotitem.setTooltip('...')

for a consistent, native look?
Reply all
Reply to author
Forward
0 new messages