import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate import griddata
# 从文件读取数据
file_path = '11111.out' # 确保文件路径正确,或者调整为正确的文件路径
df = pd.read_csv(file_path, sep='\s+', header=None)
# 定义一个函数来绘制三维曲面图
def surface_3D(df, x_index, y_index, z_index):
# 生成网格数据
x = np.linspace(df[x_index].min(), df[x_index].max(), 100)
y = np.linspace(df[y_index].min(), df[y_index].max(), 100)
X, Y = np.meshgrid(x, y)
# 二维插值
points = df[[x_index, y_index]].values
values = df[z_index].values
Z = griddata(points, values, (X, Y), method='cubic')
# 绘制图形
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
surf = ax.plot_surface(X, Y, Z, cmap=cm.viridis)
# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel(f'Z{z_index - 2}')
# 添加颜色条
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
# 为每一个 Z 轴绘图
num_columns = df.shape[1]
for z_index in range(2, num_columns):
surface_3D(df, 0, 1, z_index)
|