get_xlim() - where am I going wrong

Hi,

I have 2 graphs on one figure.
Graph one - subplot(2,1,1) velocities vs ( Time or SampleIndex) depending in input args
Graph two - subplot(2,1,2) psd() - power spectrum to help us find the resonances in velocities

As system goes through a number of different states, the resonances change during the cycle, so I need to zoomIn GraphOne to an area of interest, then re-calc the power spectrum on that portion of data only.

I have something that works in the xaxis=SampleIndex scenario, but fails in the xaxis=Time scenario, I am wondering if I am going about it the right way.

The following page has 4 graphs that show what I have working now (2 graphs of velocity vs SampleNo), and the failing case (2 graphs of Velocity Vs Time)
*http://tinyurl.com/2xpyly

···

*
I'll describe what I got.

I connected my function RecalcPowerSpectrum to ReleaseButton event
            pylab.connect( 'button_release_event', RecalcPowerSpectrum)

It's ugly and crude, but if GraphOne is a velocity vs sampleIndex, the following works.
My issues start when you look at how I am slicing (if thats the right word) the array I pass into psd() and where those values come from.

def RecalcPowerSpectrum( event ): global myAxis
    global lastXstart
    global lastXend

    xlim = myAxis.get_xlim()

    xstart = int(xlim[0])
    xend = int(xlim[1])
       if xstart != lastXstart or xend != lastXend:
        print '%s event detected, re-calc power spectrum' % event.name
        lastXstart, lastXend = xstart, xend
        lastXlimits = xlim
        print 'xlim1 =', xstart
        print 'xlim2 =', xend pylab.cla()
        if secondGraph == 'fft':# (plot UDR1 points)
            Fs = 2 * targetvel
            fftdata = pylab.psd(vel[2*xstart:2*xend], NFFT=winsize, Fs=Fs, detrend=pylab.mlab.detrend_linear, window=pylab.mlab.window_hanning, noverlap=overlap)
        else: #must be a fft2 (plot UDR2 points)
            Fs = targetvel
            fftdata = pylab.psd(combined[xstart:xend], NFFT=winsize, Fs=Fs, detrend=pylab.mlab.detrend_linear, window=pylab.mlab.window_hanning, noverlap=overlap)

This fails when I run my program to plot with xaxis in time as the values returned by get_xlim() are small (0.46408074411240841, 0.53334652681575301), once I int() these values they are both 0

Clearly I am trying to work with the wrong values. I actually want to get the indexes of these values. So I can pass the proper slice of the original array into the psd function.

So my questions.
1) Is there a way to get the indexes (not the values) of the portion of the line actually showing on screen
2) Is there a way to get a copy of the portion of the data displayed on screen (in a new array) ?
3) Is there a better way of linking psd so it performs the spectrum analysis only on the portion of data on screen?

*
*Thanks for your help
Steve

Just as a general comment - perhaps
not useful in your case - we have the same sort of requirement and find
the periodogram facility (specgram) in pylab very useful for showing the
change in PSD over time - highlighting resonances and how they vary in
proportion to other functions of time (plotted on sub-graphs).

Stephen George <steve_geo@…887…>

Sent by: matplotlib-users-bounces@lists.sourceforge.net
14/06/2007 03:52 PM

To

matplotlib Matplotlib-users@lists.sourceforge.net
cc

Subject

[Matplotlib-users] get_xlim() - where
am I going wrong

`

Hi,

I have 2 graphs on one figure.

Graph one - subplot(2,1,1) velocities vs ( Time or SampleIndex)

depending in input args

Graph two - subplot(2,1,2) psd() - power spectrum to help us find
the

resonances in velocities

As system goes through a number of different states, the resonances

change during the cycle, so I need to zoomIn GraphOne to an area of

interest, then re-calc the power spectrum on that portion of data only.

I have something that works in the xaxis=SampleIndex scenario, but fails

in the xaxis=Time scenario, I am wondering if I am going about it the

right way.

The following page has 4 graphs that show what I have working now (2

graphs of velocity vs SampleNo), and the failing case (2 graphs of

Velocity Vs Time)

*http://tinyurl.com/2xpyly

···

I’ll describe what I got.

I connected my function RecalcPowerSpectrum to ReleaseButton event

        pylab.connect( 'button_release_event',

RecalcPowerSpectrum)

It’s ugly and crude, but if GraphOne is a velocity vs sampleIndex, the

following works.

My issues start when you look at how I am slicing (if thats the right

word) the array I pass into psd() and where those values come from.

def RecalcPowerSpectrum( event ):

global myAxis

global lastXstart

global lastXend

xlim = myAxis.get_xlim()

xstart = int(xlim[0])

xend = int(xlim[1])



if xstart != lastXstart or xend != lastXend:

    print '%s event detected, re-calc power spectrum'

% event.name

    lastXstart, lastXend = xstart, xend

    lastXlimits = xlim

    print 'xlim1 =', xstart

    print 'xlim2 =', xend      

    pylab.cla()

    if secondGraph == 'fft':# (plot UDR1 points)

        Fs = 2 * targetvel

        fftdata = pylab.psd(vel[2*xstart:2*xend],

NFFT=winsize,

Fs=Fs, detrend=pylab.mlab.detrend_linear,

window=pylab.mlab.window_hanning, noverlap=overlap)

    else: #must be a fft2 (plot UDR2 points)

        Fs = targetvel

        fftdata = pylab.psd(combined[xstart:xend],

NFFT=winsize,

Fs=Fs, detrend=pylab.mlab.detrend_linear,

window=pylab.mlab.window_hanning, noverlap=overlap)

This fails when I run my program to plot with xaxis in time as the

values returned by get_xlim() are small (0.46408074411240841,

0.53334652681575301), once I int() these values they are both 0

Clearly I am trying to work with the wrong values. I actually want to

get the indexes of these values. So I can pass the proper slice of the

original array into the psd function.

So my questions.

  1. Is there a way to get the indexes (not the values) of the portion of

the line actually showing on screen

  1. Is there a way to get a copy of the portion of the data displayed on

screen (in a new array) ?

  1. Is there a better way of linking psd so it performs the spectrum

analysis only on the portion of data on screen?

*Thanks for your help

Steve


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


This email has been scanned by the MessageLabs Email Security System.

For more information please visit http://www.messagelabs.com/email


`

UNITED GROUP

This email message is the property of United Group. The information in this email is confidential and may be legally privileged. It is intended solely for the addressee. Access to this email by anyone else is unauthorised. If you are not the intended recipient, you may not disclose, copy or distribute this email, nor take or omit to take any action in reliance on it. United Group accepts no liability for any damage caused by this email or any attachments due to viruses, interference, interception, corruption or unauthorised access.

If you have received this email in error, please notify United Group immediately by email to the sender’s email address and delete this document.

So my questions.
1) Is there a way to get the indexes (not the values) of the portion of
the line actually showing on screen
2) Is there a way to get a copy of the portion of the data displayed on
screen (in a new array) ?
3) Is there a better way of linking psd so it performs the spectrum
analysis only on the portion of data on screen?

I just updated the span selector example in svn to show you how to do
this. You select a region in the upper graph with a mouse drag, and
it plots the detail of the selected region in the lower graph. You
should be able to modify this to plot the psd of the selected region,
etc....

#!/usr/bin/env python
"""
The SpanSelector is a mouse widget to select a xmin/xmax range and plot the
detail view of the selected region in the lower axes
"""
import numpy as npy
from pylab import figure, show
from matplotlib.widgets import SpanSelector

fig = figure(figsize=(8,6))
ax = fig.add_subplot(211, axisbg='#FFFFCC')

x = npy.arange(0.0, 5.0, 0.01)
y = npy.sin(2*npy.pi*x) + 0.5*npy.random.randn(len(x))

ax.plot(x, y, '-')
ax.set_ylim(-2,2)
ax.set_title('Press left mouse button and drag to test')

ax2 = fig.add_subplot(212, axisbg='#FFFFCC')
line2, = ax2.plot(x, y, '-')

def onselect(xmin, xmax):
    indmin, indmax = npy.searchsorted(x, (xmin, xmax))
    indmax = min(len(x)-1, indmax)

    thisx = x[indmin:indmax]
    thisy = y[indmin:indmax]
    line2.set_data(thisx, thisy)
    ax2.set_xlim(thisx[0], thisx[-1])
    ax2.set_ylim(thisy.min(), thisy.max())
    fig.canvas.draw()

# set useblit True on gtkagg for enhanced performance
span = SpanSelector(ax, onselect, 'horizontal', useblit=False,
                    rectprops=dict(alpha=0.5, facecolor='red') )

show()

span_selector.py (1.06 KB)

···

On 6/14/07, Stephen George <steve_geo@...887...> wrote:

*
*Thanks for your help
Steve

-------------------------------------------------------------------------
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

WOW - I delayed my bed time when I saw this come in.
It certainly seems to do what I want in your example.

Tomorrow at work I'll try and fit it into my frame work, and see what happens.

Thanks a lot,
Steve

John Hunter wrote:

···

On 6/14/07, Stephen George <steve_geo@...887...> wrote:

So my questions.
1) Is there a way to get the indexes (not the values) of the portion of
the line actually showing on screen
2) Is there a way to get a copy of the portion of the data displayed on
screen (in a new array) ?
3) Is there a better way of linking psd so it performs the spectrum
analysis only on the portion of data on screen?

I just updated the span selector example in svn to show you how to do
this. You select a region in the upper graph with a mouse drag, and
it plots the detail of the selected region in the lower graph. You
should be able to modify this to plot the psd of the selected region,
etc....

#!/usr/bin/env python
"""
The SpanSelector is a mouse widget to select a xmin/xmax range and plot the
detail view of the selected region in the lower axes
"""
import numpy as npy
from pylab import figure, show
from matplotlib.widgets import SpanSelector

fig = figure(figsize=(8,6))
ax = fig.add_subplot(211, axisbg='#FFFFCC')

x = npy.arange(0.0, 5.0, 0.01)
y = npy.sin(2*npy.pi*x) + 0.5*npy.random.randn(len(x))

ax.plot(x, y, '-')
ax.set_ylim(-2,2)
ax.set_title('Press left mouse button and drag to test')

ax2 = fig.add_subplot(212, axisbg='#FFFFCC')
line2, = ax2.plot(x, y, '-')

def onselect(xmin, xmax):
   indmin, indmax = npy.searchsorted(x, (xmin, xmax))
   indmax = min(len(x)-1, indmax)

   thisx = x[indmin:indmax]
   thisy = y[indmin:indmax]
   line2.set_data(thisx, thisy)
   ax2.set_xlim(thisx[0], thisx[-1])
   ax2.set_ylim(thisy.min(), thisy.max())
   fig.canvas.draw()

# set useblit True on gtkagg for enhanced performance
span = SpanSelector(ax, onselect, 'horizontal', useblit=False,
                   rectprops=dict(alpha=0.5, facecolor='red') )

show()

*
*Thanks for your help
Steve

-------------------------------------------------------------------------

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

------------------------------------------------------------------------

#!/usr/bin/env python
"""
The SpanSelector is a mouse widget to select a xmin/xmax range and plot the
detail view of the selected region in the lower axes
"""
import numpy as npy
from pylab import figure, show
from matplotlib.widgets import SpanSelector

fig = figure(figsize=(8,6))
ax = fig.add_subplot(211, axisbg='#FFFFCC')

x = npy.arange(0.0, 5.0, 0.01)
y = npy.sin(2*npy.pi*x) + 0.5*npy.random.randn(len(x))

ax.plot(x, y, '-')
ax.set_ylim(-2,2)
ax.set_title('Press left mouse button and drag to test')

ax2 = fig.add_subplot(212, axisbg='#FFFFCC')
line2, = ax2.plot(x, y, '-')

def onselect(xmin, xmax):
    indmin, indmax = npy.searchsorted(x, (xmin, xmax))
    indmax = min(len(x)-1, indmax)

    thisx = x[indmin:indmax]
    thisy = y[indmin:indmax]
    line2.set_data(thisx, thisy)
    ax2.set_xlim(thisx[0], thisx[-1])
    ax2.set_ylim(thisy.min(), thisy.max())
    fig.canvas.draw()

# set useblit True on gtkagg for enhanced performance
span = SpanSelector(ax, onselect, 'horizontal', useblit=False,
                    rectprops=dict(alpha=0.5, facecolor='red') )

show()

Try setting the useblit=True parameter in the SpanSelector -- you'll
get much better responsiveness. I accidentally left it off in the
code I posted.

JDH

···

On 6/14/07, steve george <steve_geo@...887...> wrote:

WOW - I delayed my bed time when I saw this come in.
It certainly seems to do what I want in your example.

Tomorrow at work I'll try and fit it into my frame work, and see what
happens.

Hi John,

This is absolutely fantastic, does everything I want, 1/2 hr after getting to work, I had it up and running in my application.

I do have a question though.

I had not seen mention of SpanSelector before in documentation.
And I do feel confusion about where to find the best documentation.

I look on the web site, and I can see a pylab API http://matplotlib.sourceforge.net/pylab_commands.html
I can see another page with MatPlotLib interface http://matplotlib.sourceforge.net/matplotlib.pylab.html#-cla
both of which make no mention of SpanSelector

After seeing you use this command I had another look on web site and saw a link to API (pdf) http://matplotlib.sourceforge.net/api.pdf
Which does have a section on SpanSelector

I did a search on the Wiki and couldn't find an entry for SpanSelector

On the forums I have heard about an OO interface.

I am Sooo confused where I should go and look for documentation now, .... how can I learn little gems like this?, Which interface should I use?

Love using MatPlotLib/pylab, whatever it's called, I've found it quick to get something up and running, the forums are great for snippets of info
and I would love to get a deeper understanding of the capabilities of this tool.

Thanks
Steve

John Hunter wrote:

···

On 6/14/07, steve george <steve_geo@...887...> wrote:

WOW - I delayed my bed time when I saw this come in.
It certainly seems to do what I want in your example.

Tomorrow at work I'll try and fit it into my frame work, and see what
happens.

Try setting the useblit=True parameter in the SpanSelector -- you'll
get much better responsiveness. I accidentally left it off in the
code I posted.

JDH

Sorry for the delayed response. The best place to start is in the
matplotlib examples directory. There are some 200 odd examples

johnh@...1653...:examples> find . -name "*.py"|wc
     233 233 4420

Almost every feature in mpl is illustrated there with working example
code. Yes it would be nice to have a comprehensive user's guide, but
until then, the path to enlightenment is to work through the examples.
In particular, in the examples/widgets dir, you'll find
widgets/span_selector.py

Hope this helps,
JDH

···

On 6/14/07, Stephen George <steve_geo@...887...> wrote:

This is absolutely fantastic, does everything I want, 1/2 hr after
getting to work, I had it up and running in my application.

I do have a question though.

I had not seen mention of SpanSelector before in documentation.
And I do feel confusion about where to find the best documentation.