3D array series of 3D array in numpy

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

What I want to do is use imshow() to display only one fragment of a three-dimensional array at a time, so that I can "go through" the 3D array to see different points of the image.

The slice command is simple: array[:,:,x] , but I don’t see the ability to display each fragment at least at a time. Anyone have suggestions other than manually changing the program file every time? Can this be done somehow interactively?

+7
source share
1 answer

I actually wrote the code to do 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 three-dimensional array, I will tell you which fragment is displayed. You can move through the slices using the arrow keys as long as you have the selected plot.

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

See also Interactive matplotlib graph with two sliders for an example of how to do this using gui sliders.

+3
source

All Articles