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),
ax = Axes3D(fig)
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)
解决方案
I did a 3d surface plot overlay on top of a background image once:
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)
show()
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: