简体   繁体   English

Python:在2D图像上绘制3D形状

[英]Python: plot 3D shape over 2D image

I'm trying to plot a 3D shape with a 2D image overlaying the xy-plane. 我正在尝试绘制3D形状,并在xy平面上覆盖2D图像。 I've only just started working with python, so this is more challenging than it ought to be. 我才刚刚开始使用python,所以这比应该的更具挑战性。

This question here addresses what I am trying to do : Image overlay in 3d plot using python . 这里的问题解决了我想做的事情: 使用python在3d图中叠加图像 But when I run the provided code I get the following error: 但是,当我运行提供的代码时,出现以下错误:

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'

The image I am using is stored in the same folder as my 'test.py.' 我使用的图像与“ test.py”存储在同一文件夹中。 The question I referenced above uses an image from get_sample_data, but if I edit it to use my image, the code is as follows: 我上面提到的问题使用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()

I get the same error whether I use get_sample_data or my own image. 无论我使用get_sample_data还是我自己的图像,都会出现相同的错误。 Any suggestions of what I can change? 关于我可以改变的任何建议吗? Thanks! 谢谢!

该错误似乎是因为plot_surface期望使用'Z'参数的数组,但是您给它指定了整数10。(因此错误'int'对象没有属性'shape')

You're not loading your image correctly - read_png() need a file object as its input, not a file path. 您没有正确加载图像read_png()需要一个文件对象作为输入,而不是文件路径。 Try this: 尝试这个:

 f = open('milkyway.png','r')
 img = read_png(f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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