Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot an image file on a 3D graph surface using Python? - not plotting as a flat plane

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?

like image 597
SteveResearch Avatar asked May 13 '15 09:05

SteveResearch


2 Answers

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

like image 120
nicoguaro Avatar answered Sep 17 '22 05:09

nicoguaro


Take a look at the answer to this question. They use a fixed z, but you could substitute that with your topographic data to get the desired effect.

like image 45
sarwar Avatar answered Sep 21 '22 05:09

sarwar