Python plot - stacked image slices

Harry Lime picture Harry Lime · Mar 23, 2013 · Viewed 14.7k times · Source

I have a series of basic 2D images (3 for simplicity for now) and these are related to each other, analogous to frames from a movie:

Within python how may I stack these slices on top of each other, as in image1->image2->image-3? I'm using pylab to display these images. Ideally an isometric view of the stacked frames would be good or a tool allowing me to rotate the view within code/in rendered image.

Any assistance appreciated. Code and images shown:

from PIL import Image
import pylab
  
fileName = "image1.png"
im = Image.open(fileName)
pylab.axis('off')
pylab.imshow(im)
pylab.show()

Image1.png here Image2.png here Image3.png here

Answer

tom10 picture tom10 · Mar 23, 2013

You can't do this with imshow, but you can with contourf, if that will work for you. It's a bit of a kludge though:

enter image description here

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.gca(projection='3d')

x = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, x)
Z = np.sin(X)*np.sin(Y)

levels = np.linspace(-1, 1, 40)

ax.contourf(X, Y, .1*np.sin(3*X)*np.sin(5*Y), zdir='z', levels=.1*levels)
ax.contourf(X, Y, 3+.1*np.sin(5*X)*np.sin(8*Y), zdir='z', levels=3+.1*levels)
ax.contourf(X, Y, 7+.1*np.sin(7*X)*np.sin(3*Y), zdir='z', levels=7+.1*levels)

ax.legend()
ax.set_xlim3d(0, 1)
ax.set_ylim3d(0, 1)
ax.set_zlim3d(0, 10)

plt.show()

The docs of what's implemented in 3D are here.

As ali_m suggested, if this won't work for you, if you can imagine it you can do it with VTk/MayaVi.