Thanks for the pointer Matthias,
That is exactly what I have been looking for.
I use the code from the RectangleSelector class help with your suggested
code. I know that I have to update y-axis accordingly to x values such that
their positions and sizes must much so that I can plot them in a new plot.
And I know that the answer lies in a mask; I have to create a mask from
x_new and apply it to y. Do you have any hint on this?
Another point is do you have any idea how to save values from inside
onselect action?
For some reason my ipython session doesn't remember values after I run the
given script:
from matplotlib.widgets import RectangleSelector
from pylab import *
def onselect(eclick, erelease):
# eclick and erelease are matplotlib events at press and release
print ' startposition : (%f, %f)' % (eclick.xdata, eclick.ydata)
print ' endposition : (%f, %f)' % (erelease.xdata, erelease.ydata)
print ' used button : ', eclick.button
xmin = min(eclick.xdata, erelease.xdata)
xmax = max(eclick.xdata, erelease.xdata)
ymin = min(eclick.ydata, erelease.ydata)
ymax = max(eclick.ydata, erelease.ydata)
x_new = x[(x>= xmin) & (x <= xmax)]
#mask = [x == x_new[i] for i in range(len(x_new))]
#print mask
#print len(x_new)
#print len(y_new)
#fig_new = figure()
#plot(x_new, y_new)
#fig_new.show()
def toggle_selector(event):
print ' Key pressed.'
if event.key in ['Q', 'q'] and toggle_selector.RS.active:
print ' RectangleSelector deactivated.'
toggle_selector.RS.set_active(False)
if event.key in ['A', 'a'] and not toggle_selector.RS.active:
print ' RectangleSelector activated.'
toggle_selector.RS.set_active(True)
x = arange(100)/(99.0)
y = sin(x)
fig = figure
ax = subplot(111)
ax.plot(x,y)
toggle_selector.RS = RectangleSelector(ax, onselect, drawtype='box')
connect('key_press_event', toggle_selector)
show()
Gökhan
On Fri, Apr 17, 2009 at 2:31 AM, Matthias Michler
<matthiasmich...@gmx.net>wrote:
> Hi Gökhan,
>
> I recommend you to use matplotlib.widgets.RectangleSelector instead of the
> zoom functionality to select the data (An example can be found at
> http://matplotlib.sourceforge.net/examples/widgets/rectangle_selector.html).
> This will return you the x and y-coordinate of button press and button
> release
> event and with that you can take a portion of your data.
> Something like the following could be a starting point:
> x_min = min(eclick.xdata, erelease.xdata)
> x_max = max(eclick.xdata, erelease.xdata)
> x_new = x[(x>= x_min) & (x <= x_max)]
>
> where eclick and erelease correspond to the click and release event of the
> rectangle selection (see the example below).
>
> Opening a new figure after show can be achieved by:
>
> fig_new = plt.figure()
> # some plotting
> fig_new.show() # show up the new figure
>
>
> best regards Matthias
>
>
> yet another example for the usage of the RectangleSelector copied from its
> class documentation:
>
> """
> Select a min/max range of the x axes for a matplotlib Axes
>
> Example usage::
>
> from matplotlib.widgets import RectangleSelector
> from pylab import *
>
> def onselect(eclick, erelease):
> 'eclick and erelease are matplotlib events at press and release'
> print ' startposition : (%f, %f)' % (eclick.xdata, eclick.ydata)
> print ' endposition : (%f, %f)' % (erelease.xdata,
> erelease.ydata)
> print ' used button : ', eclick.button
>
> def toggle_selector(event):
> print ' Key pressed.'
> if event.key in ['Q', 'q'] and toggle_selector.RS.active:
> print ' RectangleSelector deactivated.'
> toggle_selector.RS.set_active(False)
> if event.key in ['A', 'a'] and not toggle_selector.RS.active:
> print ' RectangleSelector activated.'
> toggle_selector.RS.set_active(True)
>
> x = arange(100)/(99.0)
> y = sin(x)
> fig = figure
> ax = subplot(111)
> ax.plot(x,y)
>
> toggle_selector.RS = RectangleSelector(ax, onselect,
> drawtype='line')
> connect('key_press_event', toggle_selector)
> show()
> """
>
> On Friday 17 April 2009 02:26:51 Gökhan SEVER wrote:
> > Hello,
> >
> > A quick question:
> >
> > I am using two numpy arrays to plot the figure shown in attachment. Is it
> > possible to get array indices of selected X-axes while using the zoom
> > function? Later I can create a new figure from this selected portion
> > instead of the same figure and/or apply an analysis.
> >
> > Thank you.
>
>
> ------------------------------------------------------------------------------
> Stay on top of everything new and different, both inside and
> around Java (TM) technology - register by April 22, and save
> $200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
> 300 plus technical and hands-on sessions. Register today.
> Use priority code J9JMT32. http://p.sf.net/sfu/p
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today.
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users