0

我正在尝试使用覆盖 xy 平面的 2D 图像绘制 3D 形状。我才刚刚开始使用 python,所以这比它应该的更具挑战性。

这里的这个问题解决了我想要做的事情: 使用 python 在 3d 绘图中的图像叠加。但是当我运行提供的代码时,出现以下错误:

File "test.py", line 13, in <module>
ax.plot_surface(x, y, 10, rstride=5, cstride=5, facecolors=img)
File "/usr/lib64/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 663, in plot_surface
rows, cols = Z.shape
AttributeError: 'int' object has no attribute 'shape'

我使用的图像存储在与我的“test.py”相同的文件夹中。我上面引用的问题使用了来自 get_sample_data 的图像,但如果我编辑它以使用我的图像,代码如下:

from pylab import *
from mpl_toolkits.mplot3d import Axes3D
from matplotlib._png import read_png

img = read_png('milkyway.png')

x, y = ogrid[0:img.shape[0], 0:img.shape[1]]
ax = gca(projection='3d')
ax.plot_surface(x, y, 10, rstride=5, cstride=5, facecolors=img)
show()

无论我使用 get_sample_data 还是我自己的图像,我都会遇到同样的错误。关于我可以改变什么的任何建议?谢谢!

4

2 回答 2

0

该错误似乎是因为 plot_surface 期望为“Z”参数提供一个数组,但您已给它整数 10。(因此错误“int”对象没有属性“shape”)

于 2013-07-10T22:01:34.850 回答
0

您没有正确加载图像 -read_png()需要一个文件对象作为其输入,而不是文件路径。尝试这个:

 f = open('milkyway.png','r')
 img = read_png(f)
于 2013-07-10T22:02:38.810 回答