问题描述
我有一个3元组的列表,表示3D空间中的一组点.我想绘制一个覆盖所有这些点的表面.
I have a list of 3-tuples representing a set of points in 3D space. I want to plot a surface that covers all these points.
mplot3d
包中的plot_surface
函数要求X,Y和Z作为2d数组作为参数. plot_surface
是用于绘制表面的正确函数吗?如何将数据转换为所需的格式?
The plot_surface
function in the mplot3d
package requires as arguments X,Y and Z to be 2d arrays. Is plot_surface
the right function to plot surface and how do I transform my data into the required format?
data = [(x1,y1,z1),(x2,y2,z2),.....,(xn,yn,zn)]
推荐答案
对于曲面,它与3元组列表有点不同,您应该在2d数组中为该域传递网格.
For surfaces it's a bit different than a list of 3-tuples, you should pass in a grid for the domain in 2d arrays.
如果您拥有的只是3d点列表,而不是某些函数f(x, y) -> z
,那么您将遇到问题,因为有多种方法可以将3d点云三角化为曲面.
If all you have is a list of 3d points, rather than some function f(x, y) -> z
, then you will have a problem because there are multiple ways to triangulate that 3d point cloud into a surface.
这是一个光滑的表面示例:
Here's a smooth surface example:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
# Axes3D import has side effects, it enables using projection='3d' in add_subplot
import matplotlib.pyplot as plt
import random
def fun(x, y):
return x**2 + y
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = y = np.arange(-3.0, 3.0, 0.05)
X, Y = np.meshgrid(x, y)
zs = np.array(fun(np.ravel(X), np.ravel(Y)))
Z = zs.reshape(X.shape)
ax.plot_surface(X, Y, Z)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
这篇关于matplotlib中的曲面图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!