如何从 pandas 列绘制曲面图

问题描述 投票:0回答:2

我正在尝试评估我的面积测量值。但是,我无法从 CSV 文件中的点(Position_x、Position_y、Position_z)创建一个区域。

我总是得到错误:ValueError:参数 Z 必须是二维的。并且不知道如何修复它。

有人可以帮我解决这个问题吗?我在下面给出了一个例子。

CSV 文件

Position Y,Position X,Mediane
1.5,0.5,-69.00
1.5,1.0,-67.00
1.5,1.5,-68.00
1.5,2.0,-67.00
1.5,2.5,-63.00
1.5,3.0,-55.50
1.5,3.5,-59.00
1.5,4.0,-62.00
1.5,4.5,-65.00
1.5,5.0,-68.00
1.5,5.5,-65.00
1.5,6.0,-69.00
2,0.5,-72.00
2,1.0,-74.00
2,1.5,-67.00
2,2.0,-71.00
2,2.5,-66.00

Python代码:

import numpy as np
import matplotlib.pyplot as plt 
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
import pandas as pd
from sys import exit

df = pd.read_csv(r"C:\Users\ramsa\OneDrive\Desktop\Messungen\Auswertungen\Test_Auswertung.csv")
def update_lines(num, data, line):
    line.set_data(data[0:2, :num])    
    line.set_3d_properties(data[2, :num])    
    return line

fig = plt.figure(figsize = (12,10))
ax = plt.axes(projection='3d')

x = df['Position X']
y = df['Position Y']

X, Y = np.meshgrid(x, y)
Z = df['Mediane']

surf = ax.plot_surface(X, Y, Z, cmap = plt.cm.cividis)

ax.set_xlabel('x', labelpad=20)
ax.set_ylabel('y', labelpad=20)
ax.set_zlabel('z', labelpad=20)

fig.colorbar(surf, shrink=0.5, aspect=8)

plt.show()
python matplotlib surface mplot3d matplotlib-3d
2个回答
2
投票

您收到的错误消息是由于

ax.plot_surface
需要 2D 数组用于
Z
,而您提供的是一维数组。

np.meshgrid
函数从x和y向量生成一个二维网格,所以我们需要确保我们的
Z
也对应于这个网格的形状。

但是,CSV 文件中的数据不会形成

plot_surface
功能运行所必需的规则网格。您可以使用
scipy.interpolate.griddata
函数将不规则间隔的数据插入到规则网格中。

这里是你如何做到的:

import numpy as np
import matplotlib.pyplot as plt 
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
import pandas as pd
from sys import exit
from scipy.interpolate import griddata

df = pd.read_csv(r"C:\Users\ramsa\OneDrive\Desktop\Messungen\Auswertungen\Test_Auswertung.csv")

fig = plt.figure(figsize = (12,10))
ax = plt.axes(projection='3d')

# Construct the grid.
x = df['Position X']
y = df['Position Y']
z = df['Mediane']

# Interpolate Z values on 2D grid (Creating a 2D representation of Z values)
xi = np.linspace(x.min(), x.max(), len(x.unique()))
yi = np.linspace(y.min(), y.max(), len(y.unique()))
xi, yi = np.meshgrid(xi, yi)
zi = griddata((x, y), z, (xi, yi), method='cubic')

# Draw the surface plot
surf = ax.plot_surface(xi, yi, zi, cmap = plt.cm.cividis)

ax.set_xlabel('x', labelpad=20)
ax.set_ylabel('y', labelpad=20)
ax.set_zlabel('z', labelpad=20)

fig.colorbar(surf, shrink=0.5, aspect=8)

plt.show()

在此代码中,

griddata
函数用于创建 Z 值的二维表示,它适合我们使用
np.meshgrid
创建的二维网格。

请注意,

method='cubic'
用于对值进行插值。此方法适用于平滑数据,但可能会导致非平滑数据或具有大梯度的数据出现伪影。在这种情况下,您可能想尝试
method='linear'
method='nearest'
代替。


1
投票

解决方案是。

import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# CSV-Datei einlesen
data = pd.read_csv(r"C:\Users\ramsa\OneDrive\Desktop\Messungen\Auswertungen\Test_Auswertung.csv")

# Daten extrahieren
position_y = data['Position Y']
position_x = data['Position X']
mediane = data['Mediane']

# Z-Werte sortieren
sorted_indices = mediane.argsort()
position_y = position_y[sorted_indices]
position_x = position_x[sorted_indices]
mediane = mediane[sorted_indices]

# 3D-Plot erstellen
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(position_x, position_y, mediane, cmap='viridis')

# Achsenbeschriftung
ax.set_xlabel('Position X')
ax.set_ylabel('Position Y')
ax.set_zlabel('Mediane')

# Plot anzeigen
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.