如何使用Python在3D图形表面上绘制图像文件? -不绘制为平面 [英] How to plot an image file on a 3D graph surface using Python? - not plotting as a flat plane

查看:196
本文介绍了如何使用Python在3D图形表面上绘制图像文件? -不绘制为平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Python在3D图形表面上绘制图像文件?

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?

推荐答案

您需要将表面的颜色更改为图像的颜色,如@sarwar建议的示例所示.

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()

这就是结果

这篇关于如何使用Python在3D图形表面上绘制图像文件? -不绘制为平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆