Thanks for elegant trick Matthias.
I have modified onselect function following your suggestions, and it is
working as I wanted it to be. Select a portion and get a zoomed view in a
new figure.
def onselect(eclick, erelease):
# eclick and erelease are matplotlib events at press and release
global x_new
global y_new
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)
indices = (x >= xmin) & (x <= xmax) & (y >= ymin) & (y <= ymax)
x_new = x[indices]
y_new = y[indices]
fig_new = figure()
plot(x_new, y_new)
fig_new.show()
There is still a minute point, that I would like mention again. Even though
I used global style variables I can't still see them when I quit the
program.
What I do is run the file within IPython by run command. In my case the file
is 'rect.py.' and I do run rect.py. When I quit and do whos querry, I can't
see global x_new nor y_new at the resulting lines:
In [13]: whos
Variable Type Data/Info
--------------------------------------------
RectangleSelector classobj matplotlib.widgets.RectangleSelector
ax AxesSubplot Axes(0.125,0.1;0.775x0.8)
fig function <function figure at 0xa752c6c>
onselect function <function onselect at 0xac0ec34>
toggle_selector function <function toggle_selector at 0xac0ef0c>
x ndarray 100: 100 elems, type `float64`, 800 bytes
y ndarray 100: 100 elems, type `float64`, 800 bytes
For the masking, I was planning to use a masking scheme as given below. Let
say I have an array which "a"
In [14]: a = arange(5)
In [15]: a
Out[15]: array([0, 1, 2, 3, 4])
and my secondary array is "b"
In [16]: b = array([2,3])
What I want to do is to mask a with b values and get an array of:
[array([False, False, True, True, False], dtype=bool)]
That is just an manually created array. I still don't know how to do this
programmatically in Pythonic fashion.
Again thanks for your help.
Gökhan
On Mon, Apr 20, 2009 at 4:09 AM, Matthias Michler
<matthiasmich...@gmx.net>wrote:
> Hi Gökhan,
>
> On Friday 17 April 2009 20:21:00 Gökhan SEVER wrote:
> > 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?
>
> Unfortunately I'm not familiar with numpy masks, but what I would do is:
>
> xmin = min(eclick.xdata, erelease.xdata)
> xmax = max(eclick.xdata, erelease.xdata)
> ymin = min(eclick.ydata, erelease.ydata)
> ymax = max(eclick.ydata, erelease.ydata)
> # indices inside x-range
> indices = (x>= xmin) & (x <= xmax)
> # OR: indices for data inside the selected rectangle
> #indices = (x>= xmin) & (x <= xmax) & (y>=ymin) & (y<=ymax)
> xnew = x[indices]
> ynew = y[indices]
>
> > Another point is do you have any idea how to save values from inside
> > onselect action?
>
> What do you mean by saving?
> Saving to disk?
> # for ascii format I use:
> from scipy.io import write_array
>
> # for numpy arrays you can use
> import numpy as np
> a = np.arange(10)
> a.tofile # Write array to a file as text or binary.
>
> and I think there is also some Matplotlib function for this. In the module
> matplotlib.mlab, which also allows reading different types of formatted
> data.
>
> If you think of saving inside the program. You need a global variable
> (statement "global x" at the beginning of onselect) otherwise all variables
> are deleted at the end of each onselect call.
>
> > For some reason my ipython session doesn't remember values after I run
> the
> > given script:
>
> see three lines above, please.
>
> best regards Matthias
> >
> > 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.htm
> > >l). 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
>
------------------------------------------------------------------------------
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