On 6/14/07, jkitchin <[EMAIL PROTECTED]> wrote:

I just learned about the (new?) picker features in matplotlib. That is a
fantastic development!
Someone else mentioned about having a problem though when the points are
clustered together, which brings me to my question:

Is there a way to "browse" through the data points using the tab key? What I
envision is that a particular point would have a "focus" because it had
previously been picked, and then you would press a key (such as tab) and it
would move the focus to another point, in essence picking another point in
the data set.


The TAB key is a little problematic, because it is already used by
some GUI toolkits, but this kind of thing is fairly easy to do.  MPL
gives you access to the button press events, key press events, etc, so
all you have to do is connect up the callbacks to what you want your
figure to do.

Here is an example of a "data browser" which uses 'n' and 'p' to move
through the next and previous points after a mouse select.

import numpy as npy
from pylab import figure, show


X = npy.random.rand(100, 200)
xs = npy.mean(X, axis=1)
ys = npy.std(X, axis=1)

fig = figure()
ax = fig.add_subplot(211)
ax.set_title('click on point to plot time series')
line, = ax.plot(xs, ys, 'o', picker=5)  # 5 points tolerance
ax2 = fig.add_subplot(212)

class PointBrowser:
   """
   Click on a point to select and highlight it -- the data that
   generated the point will be shown in the lower axes.  Use the 'n'
   and 'p' keys to browse through the next and pervious points
   """
   def __init__(self):
       self.lastind = 0

       self.text = ax.text(0.05, 0.95, 'selected: none',
                           transform=ax.transAxes, va='top')
       self.selected,  = ax.plot([xs[0]], [ys[0]], 'o', ms=12, alpha=0.4,
                                 color='yellow', visible=False)

   def onpress(self, event):
       if self.lastind is None: return
       if event.key not in ('n', 'p'): return
       if event.key=='n': inc = 1
       else:  inc = -1


       self.lastind += inc
       self.lastind = npy.clip(self.lastind, 0, len(xs)-1)
       self.update()

   def onpick(self, event):

      if event.artist!=line: return True

      N = len(event.ind)
      if not N: return True

      # the click locations
      x = event.mouseevent.xdata
      y = event.mouseevent.ydata


      distances = npy.hypot(x-xs[event.ind], y-ys[event.ind])
      indmin = distances.argmin()
      dataind = event.ind[indmin]

      self.lastind = dataind
      self.update()

   def update(self):
       if self.lastind is None: return

       dataind = self.lastind

       ax2.cla()
       ax2.plot(X[dataind])

       ax2.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(xs[dataind], ys[dataind]),
                transform=ax2.transAxes, va='top')
       ax2.set_ylim(-0.5, 1.5)
       self.selected.set_visible(True)
       self.selected.set_data(xs[dataind], ys[dataind])

       self.text.set_text('selected: %d'%dataind)
       fig.canvas.draw()


browser = PointBrowser()

fig.canvas.mpl_connect('pick_event', browser.onpick)
fig.canvas.mpl_connect('key_press_event', browser.onpress)

show()


Can that be done in a straight-forward way? Thanks,

j
--
View this message in context: 
http://www.nabble.com/plot-data-point-browsing-tf3922973.html#a11124244
Sent from the matplotlib - users mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

import numpy as npy
from pylab import figure, show


X = npy.random.rand(100, 200)
xs = npy.mean(X, axis=1)
ys = npy.std(X, axis=1)

fig = figure()
ax = fig.add_subplot(211)
ax.set_title('click on point to plot time series')
line, = ax.plot(xs, ys, 'o', picker=5)  # 5 points tolerance
ax2 = fig.add_subplot(212)

class PointBrowser:
    """
    Click on a point to select and highlight it -- the data that
    generated the point will be shown in the lower axes.  Use the 'n'
    and 'p' keys to browse through the next and pervious points
    """
    def __init__(self):
        self.lastind = 0

        self.text = ax.text(0.05, 0.95, 'selected: none',
                            transform=ax.transAxes, va='top')
        self.selected,  = ax.plot([xs[0]], [ys[0]], 'o', ms=12, alpha=0.4,
                                  color='yellow', visible=False)
        
    def onpress(self, event):
        if self.lastind is None: return
        if event.key not in ('n', 'p'): return
        if event.key=='n': inc = 1
        else:  inc = -1
        

        self.lastind += inc
        self.lastind = npy.clip(self.lastind, 0, len(xs)-1)
        self.update()
        
    def onpick(self, event):

       if event.artist!=line: return True

       N = len(event.ind)
       if not N: return True

       # the click locations
       x = event.mouseevent.xdata
       y = event.mouseevent.ydata


       distances = npy.hypot(x-xs[event.ind], y-ys[event.ind])
       indmin = distances.argmin()
       dataind = event.ind[indmin]

       self.lastind = dataind
       self.update()

    def update(self):
        if self.lastind is None: return

        dataind = self.lastind

        ax2.cla()
        ax2.plot(X[dataind])

        ax2.text(0.05, 0.9, 'mu=%1.3f\nsigma=%1.3f'%(xs[dataind], ys[dataind]),
                 transform=ax2.transAxes, va='top')
        ax2.set_ylim(-0.5, 1.5)
        self.selected.set_visible(True)
        self.selected.set_data(xs[dataind], ys[dataind])

        self.text.set_text('selected: %d'%dataind)
        fig.canvas.draw()


browser = PointBrowser()

fig.canvas.mpl_connect('pick_event', browser.onpick)
fig.canvas.mpl_connect('key_press_event', browser.onpress)

show()
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to