[Numpy-discussion] Numpy interpolate: cut through 2D matrix

Pierre Barthelemy barthpi@gmail....
Thu Mar 1 05:35:43 CST 2012


Hello,

for a data analysis tool i am programming, i need to plot a cut through a
2D graph. I then have a 2D array, and the indices start=(start_x,start_y)
and stop=(stop_x,stop_y) that are the position of the starting point and
stop point of the cut. The code i programmed is placed on the bottom.

This code returns only value existing in the original array: if the cut
should pass between the column index i and column index i+1, it returns
anyway the value at column index i.
Is it possible to use the numpy.interpolate library to make such that when
passing between i and i+1, the function returns an interpolation of the
graph between the points [row,column] and [row,column+1] ?


def cut_matrix(array,start,stop,shift=0):
    '''
    Draws a cut through a 2D array, between the positions
start=(row,column) and stop=(row,column)
    '''
    n_row=array.shape[0]
    n_col=array.shape[1]

    if abs(start[1]-stop[1])>abs(start[0]-stop[0]):
        if stop[1]<start[1]:
            start,stop= stop,start

        col_index=arange(start[1],stop[1]+1).astype(int)

row_index=round_(linspace(start[0],stop[0],len(col_index))).astype(int)+int(shift)
        row=(linspace(start[0],stop[0],len(col_index)))
    else:
        if stop[0]<start[0]:
            start,stop= stop,start
        row_index=arange(start[0],stop[0]+1).astype(int)

col_index=round_(linspace(start[1],stop[1],len(row_index))).astype(int)+int(shift)


    if max(col_index)>n_col or min(col_index)<0:
        print 'Error: column index not in range'
        raise IndexError
        return
    if max(row_index)>n_row or min(row_index)<0:
        print 'Error: row index not in range'
        raise IndexError
        return
    return array[row_index,col_index]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120301/0318a555/attachment.html 


More information about the NumPy-Discussion mailing list