9

Is there a way to plot an image file on a 3D graph surface using Python?

I have seen a couple of ways of plotting this as a plane but I would like the image to drape over the surface of the plot. Is this possible?

SteveResearch
  • 119
  • 3
  • 6

1 Answers1

7

You need to change the color of your surface to be the color of the image as in this example suggested by @sarwar.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.cbook import get_sample_data
from matplotlib._png import read_png
import numpy as np

fn = get_sample_data("lena.png", asfileobj=False)
img = read_png(fn)

x, y = np.mgrid[0:img.shape[0], 0:img.shape[1]]

ax = plt.gca(projection='3d')
ax.plot_surface(x, y, np.sin(0.02*x)*np.sin(0.02*y), rstride=2, cstride=2,
                facecolors=img)
plt.show()

That gives as result

enter image description here

nicoguaro
  • 3,629
  • 1
  • 32
  • 57