Use RectMode to select and get data from plots?

1,170 views
Skip to first unread message

Blot Romain

unread,
Jan 24, 2014, 7:29:16 AM1/24/14
to pyqt...@googlegroups.com
Hi,

I would like to customize the setMouseMode in order to do a multiple data selection tool instead of zooming. Can you give me some advices to start a subclass or something.?

Thank a lot.

Romain

Blot Romain

unread,
Jan 24, 2014, 7:33:45 AM1/24/14
to pyqt...@googlegroups.com
I forgot to tell that I mainly work with timeseries and not images. So ROI don't seems to do what I want.
Thanks for your help

Luke Campagnola

unread,
Jan 24, 2014, 9:12:53 AM1/24/14
to pyqt...@googlegroups.com
I would not recommend doing anything with setMouseMode. Instead, you can make a ViewBox subclass that extends the mouseDragEvent method. You can handle a left-button drag to make the selection, and use the original mouseDragEvent to handle middle- and right-button drags. Something along the lines of:

  class MyViewBox(pg.ViewBox):
      def mouseDragEvent(self, ev):
          if ev.button() != QtCore.Qt.LeftButton:
              return pg.ViewBox.mouseDragEvent(self, ev)
          ev.accept()
          # handle selection here..
 


Luke

Blot Romain

unread,
Jan 27, 2014, 10:46:35 AM1/27/14
to pyqt...@googlegroups.com
Thank you for your answer!

So I can do something like this :


class MyViewBox(pg.ViewBox):
     
    def mouseDragEvent(self, ev):
       
        if ev.button() == QtCore.Qt.RightButton:
            ev.ignore()   
        else:
            pg.ViewBox.mouseDragEvent(self, ev)
        
        ev.accept()
        pos = ev.pos()
       
        if ev.button() == QtCore.Qt.RightButton:
            self.updateScaleBox(ev.buttonDownPos(), ev.pos())
           
            if ev.isFinish(): 
                self.rbScaleBox.hide()
                ax = QtCore.QRectF(Point(ev.buttonDownPos(ev.button())), Point(pos))
                ax = self.childGroup.mapRectFromParent(ax)
                MouseRectCoords =  ax.getCoords() 
                self.dataSelection(MouseRectCoords)     
            else:
                self.updateScaleBox(ev.buttonDownPos(), ev.pos())
         
    def dataSelection(self,MouseRectCoords):
        print MouseRectCoords     

However, how can I get the Items (curve, scatter ....) into dataSelection?

Thanks,

Romain

Luke Campagnola

unread,
Jan 27, 2014, 12:01:29 PM1/27/14
to pyqt...@googlegroups.com
On Mon, Jan 27, 2014 at 10:46 AM, Blot Romain <blot....@gmail.com> wrote:
Thank you for your answer!

So I can do something like this :


class MyViewBox(pg.ViewBox):
     
    def mouseDragEvent(self, ev):
       
        if ev.button() == QtCore.Qt.RightButton:
            ev.ignore()   
        else:
            pg.ViewBox.mouseDragEvent(self, ev)
        
        ev.accept()
        pos = ev.pos()
       
        if ev.button() == QtCore.Qt.RightButton:
            self.updateScaleBox(ev.buttonDownPos(), ev.pos())
           
            if ev.isFinish(): 
                self.rbScaleBox.hide()
                ax = QtCore.QRectF(Point(ev.buttonDownPos(ev.button())), Point(pos))
                ax = self.childGroup.mapRectFromParent(ax)
                MouseRectCoords =  ax.getCoords() 
                self.dataSelection(MouseRectCoords)     
            else:
                self.updateScaleBox(ev.buttonDownPos(), ev.pos())
         
    def dataSelection(self,MouseRectCoords):
        print MouseRectCoords     

Sure! It's a little awkward reusing updateScaleBox for this purpose, but I suppose it works..

 

However, how can I get the Items (curve, scatter ....) into dataSelection?

Message has been deleted

Blot Romain

unread,
Jan 29, 2014, 8:08:15 AM1/29/14
to pyqt...@googlegroups.com
Thanks again for your help, I would not have been able to think about it alone.
So I made the code attached to illustrate what I try to do. Now I can select points dragging a rectangle with the mouse right click, which is a good step for me :). However, I can not find a way to change the color of the selected points. I failed to use the scatterPlotItem attributs and classes to do it.
Do you have an idea?

Romain
DataSelect.py

Luke Campagnola

unread,
Jan 30, 2014, 11:10:38 AM1/30/14
to pyqt...@googlegroups.com


On Wed, Jan 29, 2014 at 8:05 AM, Blot Romain <blot....@gmail.com> wrote:
Thanks again for your help, I would not have been able to think about it alone. So I made the code attached to llustrate what I try to do. Now I can select points dragging a rectangle with the mouse right click, which a good step for me :). However, I can not find a way to change the color of the selected points. I failed to use the scatterPlotItem attributs and classes to do it. Do you have an idea?



I recommend you have a look at examples/ScatterPlot.py. Clicking on any of the plots there changes the outline of any points under the mouse cursor. You should be able to use the same approach..


Luke

 

Blot Romain

unread,
Feb 3, 2014, 12:40:25 PM2/3/14
to pyqt...@googlegroups.com
Hi, again you were right the scatterplot example put me the right path.

Very sorry to bother you again but I have a problem with pg.GraphicsScene.items() now. As you commented in GraphicScene.py, the original class from QtGui has a bug and it looks like it is what happen to me. The example attached previously stop to work when I increase the number of data points, exemple :

a  = np.arange(100)
b  = np.arange(100)

The problem is that pg.GraphicsScene.items doesn't always return the same number and kind of Item Object. Sometimes it works but sometimes not. Do you have an idea on how I can fix that?

thanks

Romain

Blot Romain

unread,
Feb 3, 2014, 12:48:12 PM2/3/14
to pyqt...@googlegroups.com
Also, I wonder why pg.GraphicsScene.items() doesn't return anything for a very large number of points.

Blot Romain

unread,
Feb 4, 2014, 5:25:55 AM2/4/14
to pyqt...@googlegroups.com
Oh I see that the main issue is that GraphicsScene.items doesn't like to have a QRectF in local coords.

Luke Campagnola

unread,
Feb 4, 2014, 9:14:00 PM2/4/14
to pyqt...@googlegroups.com
On Tue, Feb 4, 2014 at 5:25 AM, Blot Romain <blot....@gmail.com> wrote:
Oh I see that the main issue is that GraphicsScene.items doesn't like to have a QRectF in local coords.

Correct; this should be mapped to scene coordinates first.
Is there still an issue remaining that you need help with? I couldn't really follow..

Blot Romain

unread,
Feb 7, 2014, 9:29:54 AM2/7/14
to pyqt...@googlegroups.com
Thanks again Luke. For now I have my stuff working ok.
Reply all
Reply to author
Forward
0 new messages