从包含在三列文件中的数据生成 3d 图

generate a 3d plot from data contained in a three columns file

我有一个以这种方式构建的三列数据文件(示例):

X Y Z

0 0 0.2 
0 1 0.3
0 2 0.1
1 0 0.2
1 1 0.3
1 2 0.9
2 0 0.6
2 1 0.8
2 2 0.99

我不知道这种文件是如何命名的...但我没有找到任何使用 3d 线框或 3d 曲面图绘制此文件的示例...

编辑但是有一种方法可以用这种方式构造数据来生成线框或曲面图吗?

为了创建曲面图,您首先需要将 x、y 和 z 数据转换为二维数组。然后你就可以轻松地绘制它了:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# read out data from txt file
data = np.genfromtxt("data.txt")[1:]
x_data, y_data, z_data = data[:, 0], data[:, 1], data[:, 2]

# initialize a figure for the 3d plot
fig = plt.figure()
ax = Axes3D(fig)

# create matrix for z values
dim = int(np.sqrt(len(x_data)))
z = z_data.reshape((dim, dim))

# create matrix for the x and y points
x, y = np.arange(0, dim, 1), np.arange(0, dim, 1)
x, y = np.meshgrid(x, y)

# plot
ax.plot_surface(x, y, z, alpha=0.75)
plt.show()