【发布时间】:2019-10-21 06:51:11
【问题描述】:
我有reviewed both 的these threads,但我仍在努力从numpy 的x, y, z 坐标数组制作3D 曲面图。
我的数组如下所示:
>>> points
array([[ 322697.1875 , 3663966.5 , -30000. ],
[ 325054.34375 , 3663966.5 , -30000. ],
[ 325054.34375 , 3665679.5 , -30000. ],
[ 322697.1875 , 3665679.5 , -30000. ],
[ 322697.1875 , 3663966.5 , -27703.12304688],
[ 325054.34375 , 3663966.5 , -27703.15429688],
[ 325054.34375 , 3665679.5 , -27703.70703125],
[ 322697.1875 , 3665679.5 , -27703.67382812]])
ax.plot_surface 接受 x, y, z 点,所以我将上面的数组转换为下面的单独部分:
x = points[:, 0]
y = points[:, 1]
z = points[:, 2]
然后我将其放入网格中以传递给ax.plot_surface():
import numpy as np
X, Y, Z = np.meshgrid(x, y, z)
然后尝试绘制:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(16,10))
ax = plt.axes(projection = '3d')
ax.plot_surface(X, Y, Z, alpha=0.5)
plt.show()
当我运行它时,我收到一个错误:rows, cols = Z.shape ValueError: too many values to unpack (expected 2)。
我现在不知道该去哪里,我不期待答案,但朝着正确的方向推动会很棒。
更新:如果我在meshgrid 中不包含z,而只包含x 和y,则在运行ax.plot_surface(X, Y, z, alpha=0.5) 时会得到以下输出:
这真的很接近,但我希望所有边都被填充。只有一个显示为已填充。我添加了点坐标以显示边界。我觉得这与我正在创建的meshgrid 有关。这是X, Y的输出:
>>> X, Y = np.meshgrid(x, y)
(array([[322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
[322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
[322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
[322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
[322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
[322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
[322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
322697.1875 , 325054.34375, 325054.34375, 322697.1875 ],
[322697.1875 , 325054.34375, 325054.34375, 322697.1875 ,
322697.1875 , 325054.34375, 325054.34375, 322697.1875 ]]), array([[3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5,
3663966.5, 3663966.5],
[3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5,
3663966.5, 3663966.5],
[3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5,
3665679.5, 3665679.5],
[3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5,
3665679.5, 3665679.5],
[3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5,
3663966.5, 3663966.5],
[3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5, 3663966.5,
3663966.5, 3663966.5],
[3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5,
3665679.5, 3665679.5],
[3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5, 3665679.5,
3665679.5, 3665679.5]]))
如果我只取 x, y 唯一值,我会抛出错误:
x = np.unique(x)
y = np.unique(y)
>>> x
array([322697.1875 , 325054.34375])
>>> y
array([3663966.5, 3665679.5])
X, Y = np.meshgrid(x, y)
>>> X, Y
(array([[322697.1875 , 325054.34375],
[322697.1875 , 325054.34375]]), array([[3663966.5, 3663966.5],
[3665679.5, 3665679.5]]))
>>> ax.plot_surface(X, Y, z, alpha=0.5)
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
ax.plot_surface(X, Y, z, alpha=0.5)
File "/Users/NaN/anaconda/envs/py36/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 1586, in plot_surface
X, Y, Z = np.broadcast_arrays(X, Y, Z)
File "/Users/NaN/anaconda/envs/py36/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 259, in broadcast_arrays
shape = _broadcast_shape(*args)
File "/Users/NaN/anaconda/envs/py36/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 193, in _broadcast_shape
b = np.broadcast(*args[:32])
ValueError: shape mismatch: objects cannot be broadcast to a single shape
【问题讨论】:
-
错误出现在代码的哪一行?我猜它来自具有三个维度而不是 2 的 Z。
-
不要在网格中包含 z。
-
@tnknepp @cripcate 谢谢,我尝试只与
x和y啮合,并在ax.plot_surface分别插入z,但我收到了shape mismatch错误,如更新中所示发布 -
在您的情况下,如果您想使用单个曲面图而不是链接问题中的多个曲面图,则 X、Y 和 Z 都需要是一个 5x5 数组。稍后我可能会在那里提供答案。
-
@ImportanceOfBeingErnest 是的,实际上,我有许多多边形要绘制在一个图表中,即许多
points数组,因此我需要找到一种方法将其放入函数或循环中一次全部绘制,无需硬连线任何值
标签: python python-3.x numpy matplotlib