Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D slice series of 3D array in numpy

I have a 3D array that represents density values over cartesian space. To get a 2D image I just sum over one of the axes using sum(array,2) and then use the matplotlib function imshow(array2D) to obtain the 2D image.

What I want to do is use imshow() to display only one slice of the 3D array at a time so that I can 'page' through the 3D array to see different points of the image.

The slice command is simple: array[:,:,x] but I see no way to display every slice one at a time at least. Does anyone have suggestions other than manually changing the program file each time? Can this be done interactively somehow?

like image 235
Sebastian De Pascuale Avatar asked Jul 08 '11 06:07

Sebastian De Pascuale


People also ask

How do you slice a 2D array in python NumPy?

Slice Two-dimensional Numpy Arrays To slice elements from two-dimensional arrays, you need to specify both a row index and a column index as [row_index, column_index] . For example, you can use the index [1,2] to query the element at the second row, third column in precip_2002_2013 .

What does [: :] mean on NumPy arrays?

The [:, :] stands for everything from the beginning to the end just like for lists. The difference is that the first : stands for first and the second : for the second dimension. a = numpy. zeros((3, 3)) In [132]: a Out[132]: array([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])

Can NumPy arrays be sliced?

Slicing in python means extracting data from one given index to another given index, however, NumPy slicing is slightly different. Slicing can be done with the help of (:) . A NumPy array slicing object is constructed by giving start , stop , and step parameters to the built-in slicing function.

How do you access different rows of a multidimensional NumPy array?

In NumPy , it is very easy to access any rows of a multidimensional array. All we need to do is Slicing the array according to the given conditions. Whenever we need to perform analysis, slicing plays an important role.


1 Answers

I actually wrote code to do exactly what I think you are looking for, see if this helps:

import numpy as np
import pylab

class plotter:
    def __init__(self, im, i=0):
        self.im = im
        self.i = i
        self.vmin = im.min()
        self.vmax = im.max()
        self.fig = pylab.figure()
        pylab.gray()
        self.ax = self.fig.add_subplot(111)
        self.draw()
        self.fig.canvas.mpl_connect('key_press_event',self)

    def draw(self):
        if self.im.ndim is 2:
            im = self.im
        if self.im.ndim is 3:
            im = self.im[...,self.i]
            self.ax.set_title('image {0}'.format(self.i))

        pylab.show()

        self.ax.imshow(im, vmin=self.vmin, vmax=self.vmax, interpolation=None)


    def __call__(self, event):
        old_i = self.i
        if event.key=='right':
            self.i = min(self.im.shape[2]-1, self.i+1)
        elif event.key == 'left':
            self.i = max(0, self.i-1)
        if old_i != self.i:
            self.draw()
            self.fig.canvas.draw()


def slice_show(im, i=0):
    plotter(im, i)

Just call the show function on your 3d array, i will tell it what slice to displaying. You can step through slices with arrow keys as long as you have the plot selected.

Note, this expects arrays with shape (x, y, z), you could for example get such an array from a series of 2d arrays with np.dstack((im1, im2, ...)).

See also Interactive matplotlib plot with two sliders for a code example of doing it with gui sliders

like image 175
triplepoint217 Avatar answered Oct 29 '22 20:10

triplepoint217