简体   繁体   中英

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?

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

在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM