Skip to main content
 首页 » 编程设计

python之绘制到 plot_surface之无法将数据正确转换为所需格式

2024年10月25日8over140

我在尝试获取数据阵列的曲面图时遇到问题。 (请参阅下面的小测试数据样本)。

这是我用来测试我遇到的不同方法的基本代码。

import numpy as np 
import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
 
data = np.loadtxt('lobe1.dat') 
xs = data[:,0] 
ys = data[:,1] 
zs = data[:,2] 
 
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
 
ax.plot_surface(xs,ys,zs, rstride=1, cstride=1,color='b') 
 
ax.set_xlabel('x') 
ax.set_ylabel('y') 
ax.set_zlabel('z') 
 
ax.set_xlim([-0.08,0.08]) 
ax.set_ylim([-0.08,0.08])  
ax.set_zlim([-0.08,0.08])  
 
ax.set_aspect("equal") 
 
plt.show() 

在这个版本中,我得到了一个绘图轴,但数据丢失了。我已将其缩小到 xs、ys、zs 需要是二维数组这一事实。我可以使用 np.meshgrid 来转换 xs,ys 但我如何处理 zs?即:

xs,ys = np.meshgrid(xs, ys) 
zs = ??? 

我还遇到了解决方案 here,它建议使用下面的代码并绘制新的 X、Y、Z(可能使用一些数组排序),但这会引发

ValueError: total size of new array must be unchanged 

在调用 xs.reshape() 时。

cols = np.unique(xs).shape[0] 
X = xs.reshape(-1, cols) 
Y = ys.reshape(-1, cols) 
Z = zs.reshape(-1, cols) 

这让我很困惑,所以我很感激任何输入。通常我要处理一个大得多的数据集,但这里有一些来自 lobe1.dat 的样本数据,可以让您重现我的问题。每列分别由xs、ys和zs组成。

0.000000000000000 0.000000000000000 -0.049815118005748 
0.039186360495468 0.000000000000000 -0.038410316117142 
0.056803651667469 0.000000000000000 -0.024791146049150 
0.072097497329379 0.000000000000000 -0.007246071120158 
0.067060501396178 0.000000000000000 0.013688667526320 
0.051379603016815 0.000000000000000 0.029664027632614 
0.031789354260577 0.000000000000000 0.042385805680769 
0.000000000000000 0.000000000000000 -0.049815118005748 
0.033152869816836 0.019140818313159 -0.037523509346281 
0.045829649529645 0.026459760492807 -0.023095972443560 
0.052288905121942 0.030189013447784 -0.006068220024078 
0.050910857853398 0.029393397486334 0.011999804274721 
0.042289602909023 0.024415913623447 0.028193068606016 
0.027154245218747 0.015677510786684 0.041806695431158 
0.000000000000000 0.000000000000000 -0.049815118005748 
0.018422854617625 0.031909320218181 -0.036116018976805 
0.024307976693871 0.042102650662984 -0.021217741560168 
0.026764953958725 0.046358260118753 -0.005379958170438 
0.026279549165091 0.045517514353942 0.010728581020809 
0.022786184948649 0.039466830041721 0.026311220027814 
0.015304154209252 0.026507572657293 0.040811077891338 
0.000000000000000 0.000000000000000 -0.049815118005748 
0.000000000000000 0.036309017710339 -0.035589956059286 
0.000000000000000 0.047273292454049 -0.020631756287663 
0.000000000000000 0.051675786366447 -0.005193611943154 
0.000000000000000 0.050815730690458 0.010372717591526 
0.000000000000000 0.044489633172666 0.025686101688387 
0.000000000000000 0.030316588229639 0.040422117639519 
-0.000000000000000 0.000000000000000 -0.049815118005748 
-0.018496134334071 0.032036244410230 -0.036259675955309 
-0.024429654459698 0.042313402735548 -0.021323950621558 
-0.026908703737555 0.046607242039264 -0.005408852963170 
-0.026418888079279 0.045758856432786 0.010785465894322 
-0.022894621383867 0.039654647456911 0.026436431637941 
-0.015355646960057 0.026596760717909 0.040948391893486 
-0.000000000000000 0.000000000000000 -0.049815118005748 
-0.033286051232919 0.019217710639585 -0.037674248456914 
-0.045779044607711 0.026430543727506 -0.023070469960026 
-0.051867788406975 0.029945881599038 -0.006019348683662 
-0.050596024278383 0.029211628237049 0.011925597289441 
-0.042339044284962 0.024444458615154 0.028226029523308 
-0.027273140887785 0.015746155233209 0.041989747288558 
-0.000000000000000 0.000000000000000 -0.049815118005748 
-0.039273413070914 0.000000000000000 -0.038495644708504 
-0.055872341877765 0.000000000000000 -0.024384689134220 
-0.065935779675748 0.000000000000000 -0.006626795195273 
-0.063568190701275 0.000000000000000 0.012975802590838 
-0.051006846275802 0.000000000000000 0.029448816427848 
-0.031916472879064 0.000000000000000 0.042555297172085 
-0.000000000000000 -0.000000000000000 -0.049815118005748 
-0.033286051232919 -0.019217710639585 -0.037674248456914 
-0.045779044607711 -0.026430543727506 -0.023070469960026 
-0.051867788406975 -0.029945881599038 -0.006019348683662 
-0.050596024278383 -0.029211628237049 0.011925597289441 
-0.042339044284962 -0.024444458615154 0.028226029523308 
-0.027273140887785 -0.015746155233209 0.041989747288558 
-0.000000000000000 -0.000000000000000 -0.049815118005748 
-0.018496134334071 -0.032036244410230 -0.036259675955309 
-0.024429654459698 -0.042313402735548 -0.021323950621558 
-0.026908703737555 -0.046607242039264 -0.005408852963170 
-0.026418888079279 -0.045758856432786 0.010785465894322 
-0.022894621383867 -0.039654647456911 0.026436431637941 
-0.015355646960057 -0.026596760717909 0.040948391893486 
-0.000000000000000 -0.000000000000000 -0.049815118005748 
-0.000000000000000 -0.036309017710339 -0.035589956059286 
-0.000000000000000 -0.047273292454049 -0.020631756287663 
-0.000000000000000 -0.051675786366447 -0.005193611943154 
-0.000000000000000 -0.050815730690458 0.010372717591526 
-0.000000000000000 -0.044489633172666 0.025686101688387 
-0.000000000000000 -0.030316588229639 0.040422117639519 
0.000000000000000 -0.000000000000000 -0.049815118005748 
0.018422854617625 -0.031909320218181 -0.036116018976805 
0.024307976693871 -0.042102650662984 -0.021217741560168 
0.026764953958725 -0.046358260118753 -0.005379958170438 
0.026279549165091 -0.045517514353942 0.010728581020809 
0.022786184948649 -0.039466830041721 0.026311220027814 
0.015304154209252 -0.026507572657293 0.040811077891338 
0.000000000000000 -0.000000000000000 -0.049815118005748 
0.033152869816836 -0.019140818313159 -0.037523509346281 
0.045829649529645 -0.026459760492807 -0.023095972443560 
0.052288905121942 -0.030189013447784 -0.006068220024078 
0.050910857853398 -0.029393397486334 0.011999804274721 
0.042289602909023 -0.024415913623447 0.028193068606016 
0.027154245218747 -0.015677510786684 0.041806695431158 
0.000000000000000 -0.000000000000000 -0.049815118005748 
0.039186360495468 -0.000000000000000 -0.038410316117142 
0.056803651667469 -0.000000000000000 -0.024791146049150 
0.072097497329379 -0.000000000000000 -0.007246071120158 
0.067060501396178 -0.000000000000000 0.013688667526320 
0.051379603016815 -0.000000000000000 0.029664027632614 
0.031789354260577 -0.000000000000000 0.042385805680769 

非常感谢您提供的任何帮助。

请您参考如下方法:

other answer也适用于此,因为您已经有一个(常规!)网格。但是,采用唯一值对您的数据不起作用(显然)。相反,我查看了数据并手动决定了网格的形状,即在我们返回 x=0 之前,在 x 数据中总是有 7 个条目。所以以下工作:

data = np.loadtxt('lobe1.dat') 
xs = data[:,0].reshape((-1, 7)) 
ys = data[:,1].reshape((-1, 7)) 
zs = data[:,2].reshape((-1, 7)) 
 
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
 
ax.plot_surface(xs,ys,zs, rstride=1, cstride=1,color='b') 
 
plt.show() 

证明: