Image overlay in 3d plot using python


I have a 3d plot of lines generated by matplotlib. I want to overlay an image at a specific xy (or yz, xz) slice. How do I do that using python? Thanks.

I have a simple 3d plot code as:

fig = plt.figure(1),<br>
ax = Axes3D(fig)<br>
ax.plot(f[:,0], f[:,1], f[:,2], color='r')

I also have an image "Im" (a 2d array), so I need something like:

ax.overlay(Im, slice='xy', sliceNo=10)
11/26/2012 11:17:27 PM

Accepted Answer

I did a 3d surface plot overlay on top of a background image once:

3d surface plot on top of background image

If this is similar to what you want, I could try to make a working example out of it.

Alternatively, if you just want to display an image in 3d space, you can use a surface plot:

from pylab import *
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.cbook import get_sample_data
from matplotlib._png import read_png
fn = get_sample_data("lena.png", asfileobj=False)
img = read_png(fn)
x, y = ogrid[0:img.shape[0], 0:img.shape[1]]
ax = gca(projection='3d')
ax.plot_surface(x, y, 10, rstride=5, cstride=5, facecolors=img)

Of course, the stride values can be decreased to 1 for better image quality, but then drawing will take loooong =)

Resulting image from above code:

enter image description here

3/23/2013 9:48:24 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow