gpt4 book ai didi

python - Matplotlib-Imported PNG 不会在 3D 绘图中显示

转载 作者:行者123 更新时间:2023-12-05 08:00:10 26 4
gpt4 key购买 nike

我正在尝试使用 matplotlib 生成 3D 图形,其中 xy 平面是图像,然后在顶部绘制一些 3D 轨迹(该部分工作正常)。问题是,即使我导入的 PNG 在 imshow 上显示得很好,即使我可以在 3D 轴上绘制图像,如果我只是使用食谱中的示例,我的图像只是显示为一个没有特征的黑框。我确定我遗漏了一些小东西 - 提前致谢!

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D, art3d
from pylab import ogrid
import matplotlib.pyplot as plt

plt.ioff()
fig = plt.figure()
ay=fig.add_subplot(2,1,1)
rawim=plt.imread(r'G:\Path\myimage.png')
ay.imshow(rawim,cmap='gray')
ax=fig.add_subplot(2,1,2,projection='3d')
x,y= ogrid[0:rawim.shape[0],0:rawim.shape[1]]
ax.plot_surface(x,y,0,rstride=5,cstride=5,facecolors=rawim,cmap='gray')
ax.view_init(elev=45, azim=12)
plt.show()

输出结果如下(经过编辑以包含图像)。 enter image description here
PS 在 Spyder for Python 2.75 中运行 Matplotlib 1.2.1

编辑添加 - 我主要是从 this post 对我的方法进行建模, 所以如果不是

rawim=plt.imread(r'G:\Path\myimage.png')

我用

from matplotlib.cbook import get_sample_data
fn = get_sample_data("lena.png", asfileobj=False)
rawim=read_png(fn)

它完美地工作。我已经尝试了几个 PNG 输出,产生了几种不同的方式,但没有爱。是的,它们是介于 0-1 之间的灰度。

最佳答案

您应该为面色使用明确的颜色数组。您想要具有形状 (Nx, Ny, 4) 的东西,其中“4”维包含 RGBA 值。此外,去掉 plot_surface 调用中的 cmap='gray'。

关于python - Matplotlib-Imported PNG 不会在 3D 绘图中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18956105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com