Python: pandas 3d 数据帧到 X,Y,Z

Python: pandas 3d dataframe into X,Y,Z

我的 csv 数据由 pd.read_csv (https://www.pkks.de/contourN.dat) 读取:

         0        1         2
0     -3.0 -3.00000 -0.001395
1     -3.0 -2.97647 -0.001410
2     -3.0 -2.95294 -0.001421
3     -3.0 -2.92941 -0.001426
4     -3.0 -2.90588 -0.001427
...    ...      ...       ...
65531  3.0  2.90588 -0.001427
65532  3.0  2.92941 -0.001426
65533  3.0  2.95294 -0.001421
65534  3.0  2.97647 -0.001410
65535  3.0  3.00000 -0.001395

表示(x,y,z)坐标。我必须如何将这些数据转换为 X, Y, Z 变量才能 使用 matplotlib 绘制曲面:

ax.plot_surface(X, Y, Z, ...)
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

data = pd.read_csv('your_file.csv', sep=',', names=['X', 'Y', 'Z'], index_col=False)

x_num = len(data['X'].unique())
y_num = len(data['Y'].unique())

x = data['X'].values.reshape(x_num, -1)
y = data['Y'].values.reshape(y_num, -1)
z = data['Z'].values.reshape((x_num, y_num))

fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x, y, z)