在Python 3中绘制3D多边形 [英] Plotting 3D Polygons in Python 3
问题描述
在寻求以某种方式实际绘制3D多边形的过程中,我遇到了以下脚本(稍作修改):
In my quest to somehow get 3D polygons to actually plot, I came across the following script ( modified slightly): Plotting 3D Polygons in python-matplotlib
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import Poly3DCollection
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [0,1,1,0]
y = [0,0,1,1]
z = [0,1,0,1]
verts = [zip(x, y,z)]
ax.add_collection3d(Poly3DCollection(verts),zs=z)
plt.show()
但是当我运行它时,出现以下错误消息:
But when I run that, I get the following error message:
TypeError: object of type 'zip' has no len()
这似乎是Python 2 vs. 3的事情,因为我在Python 3中运行,并且该职位已有5年历史了.因此,我将倒数第三行更改为:
It seems that this may be a Python 2 vs. 3 thing, as I am running in Python 3, and that post is five years old. So I changed the third-to-last line to:
verts = list(zip(x, y, z))
现在,变量列表中显示了顶点,但仍然出现错误:
Now verts shows up in the variable list, but I still get an error:
TypeError: zip argument #1 must support iteration
什么?我该如何解决?
推荐答案
我在压缩过程中遇到了类似的问题.我支持这个论点,它是python 2.x vs 3.x的东西.
I've had a similar problem with the zipping. I support the thesis it is a python 2.x vs 3.x thing.
但是,我发现了一个明显可行的地方:
However, I've found somewhere that apparently works:
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [0, 1, 1, 0]
y = [0, 0, 1, 1]
z = [0, 1, 0, 1]
verts = [list(zip(x, y, z))]
print(verts)
ax.add_collection3d(Poly3DCollection(verts), zs='z')
plt.show()
因此,我进行了两项更改:
I've thus made two changes:
-
替换了该行:
from matplotlib.collections import Poly3DCollection
经过:from matplotlib.mplot3.art3d import Poly3DCollection
.
replaced the line:
from matplotlib.collections import Poly3DCollection
by:from matplotlib.mplot3.art3d import Poly3DCollection
.
我不知道您的导入语句的来源,但它似乎对我不起作用
I don't know where your import statement originates from, but it didn't seem to work for me
将行从verts = list(zip(x,y,z))
更改为verts =
[list(zip(x, y, z))]
changed the line: verts = list(zip(x,y,z))
to verts =
[list(zip(x, y, z))]
以某种方式,后者似乎起作用.刚开始使用python时,我无法提供明确的解释.但是,这里什么也没做:Poly3DCollection类要求将集合"作为第一个输入参数,因此需要一个列表列表.在这种情况下,仅给出列表,因此假定其错过了一个等级.通过向它添加另一个级别(通过[...]),它起作用了.
Somehow, the latter seems to work. Having just started myself with python, I cannot offer an iron-clad explanation. However, here goes nothing: the class Poly3DCollection requires as the first input parameter a "collection", hence a list of lists. In this case, only list is given, which is assumed thus missed a level. By adding another level to it (via the [...]) it worked.
我不知道这种解释是否合理,但是直觉上适合我;)
I've got no idea if this explanation makes sense, however it fits intuitively to me ;)
这些修改似乎可以正常工作,因为此代码创建了所需的3D多边形(我注意到,由于这是我的第一篇文章,所以我不允许发布布丁证明.... )
These modifications seem to work, as this code creates the desired 3D polygon (I've noticed that since this is my first post, I'm not allowed to post a proof-of-the-pudding figure.... )
希望这很有用,
亲切的问候
这篇关于在Python 3中绘制3D多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!