小白救助,已知三列数据怎样作三维曲面图
已知三列数据,需要画出三维曲面图,在jupyter notebook中一直画不出来{:10_266:} {:10_266:} {:10_266:}def surface_3D():
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import numpy as np
fig,ax = plt.subplots(subplot_kw={"projection":"3d"})
X = df1.loc[:,0]
Y = df1.loc[:,1]
Z1 = df1.loc[:,2]
X = np.atleast_2d(X)
Y = np.atleast_2d(Y)
Z1 = np.atleast_2d(Z1)
x = np.linspace(0,2,1000)
y = np.linspace(0,2,1000)
surf = ax.plot_surface(X, Y, Z1, cmap=cm.viridis)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z1')
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show() Eternal03 发表于 2024-4-14 19:36
就是我的XY值也不是等间距的这种也需要生成等间距的网格数据吗
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.min(), df.max(), 100)
y = np.linspace(df.min(), df.max(), 100)
X, Y = np.meshgrid(x, y)
# 二维插值
points = df[].values
values = df.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
for z_index in range(2, num_columns):
surface_3D(df, 0, 1, z_index)
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
# 示例数据
data = {
0: np.random.rand(10),# X values
1: np.random.rand(10),# Y values
2: np.random.rand(10) # Z values
}
df1 = pd.DataFrame(data)
def surface_3D(df):
# 生成网格数据
x = np.linspace(df.min(), df.max(), 100)
y = np.linspace(df.min(), df.max(), 100)
X, Y = np.meshgrid(x, y)
# 二维插值
points = df[].values
values = df.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('Z')
# 添加颜色条
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
# 调用函数
surface_3D(df1)
小甲鱼的二师兄 发表于 2024-4-14 19:20
佬,那要是向这样的文件里第一二列分别为X轴和Y轴,第三列为Z1轴,第四列为Z2轴,第五列为Z3轴......以此类推,要作出好几个几个三维曲面图该怎么办呀 Eternal03 发表于 2024-4-14 19:34
佬,那要是向这样的文件里第一二列分别为X轴和Y轴,第三列为Z1轴,第四列为Z2轴,第五列为Z3轴......以此 ...
就是我的XY值也不是等间距的这种也需要生成等间距的网格数据吗
页:
[1]