python - 如何在 3D 曲面图中绘制一个由 x、y、z 组成的 numpy 数组?
问题描述
我已经查看了这两个线程,但仍在努力从一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)
。
我现在不知道该去哪里,我不期待答案,但朝着正确的方向推进会很棒。
更新:如果我不包含z
在meshgrid
, 但只有x
andy
中,我会在运行时得到这个输出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
解决方案
数组 x、y、z 需要在二维中进行参数化。这样做的一种方法是使用球坐标,例如在立方体上绘制表面。
剩下的任务是从输入数据中提取唯一坐标。我在这里假设每个维度只有 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()
推荐阅读
- java - 具有自定义值和默认值的枚举
- node.js - 如何在不影响 Node.JS 应用程序的情况下从后台异步发送大量电子邮件?
- angular - 如何修复错误:没有可用于依赖类型的模块工厂:ContextElementDependency
- go - 从 C void 指针中检索 Golang 结构
- html - 如何将父容器高度设置为等于高度最小的子容器?
- python - 需要帮助在 python 中使用生成器打印两个列表
- .net - 在 MVC .NET 项目中使用连字符创建自定义端点
- react-native - React Native 应用程序快速构建多次,然后在 Expo 的生产模式下失败
- django - django python表单提交,从数据库中删除行并刷新页面
- kubernetes - 恢复 Kubernetes 集群