作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 reviewed both的 these threads ,但我仍在努力从 numpy
制作 3D 曲面图x, y, z
的数组坐标。
我的数组如下所示:
>>> 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)
.
z
在
meshgrid
,但只有
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 = 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
最佳答案
数组 x, y, z 需要在二维中进行参数化。这样做的一种方法是使用球面坐标,例如在 Plot surfaces on a cube .
剩下的任务是从输入数据中提取唯一坐标。我在这里假设每个维度只有 2 个不同的值。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def get_cube():
phi = np.arange(1,10,2)*np.pi/4
Phi, Theta = np.meshgrid(phi, phi)
x = np.cos(Phi)*np.sin(Theta)
y = np.sin(Phi)*np.sin(Theta)
z = np.cos(Theta)/np.sqrt(2)
return x,y,z
points = np.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.12],
[ 325054.34375 , 3663966.5 , -27703.12],
[ 325054.34375 , 3665679.5 , -27703.12],
[ 322697.1875 , 3665679.5 , -27703.12]])
ux = np.unique(points[:,0])
uy = np.unique(points[:,1])
uz = np.unique(points[:,2])
x,y,z = get_cube()
offset = lambda X, o: o[0] + (X+.5)*np.diff(o)[0]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(offset(x, ux), offset(y, uy), offset(z, uz))
plt.show()
关于python - 如何在 3D 曲面图中绘制 x、y、z 的 numpy 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56465934/
我是一名优秀的程序员,十分优秀!