2015-04-17 44 views
2

我遇到了試圖獲取我的數據數組的表面圖的問題。 (請參閱下面的小測試數據示例)。繪圖成plot_surface - 無法正確地將數據轉換爲所需格式

這是我用來測試我遇到的不同方法的基本代碼。

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() 

有了這個版本,我得到了一個繪圖軸,但數據丟失了。我已經縮小到事實xsys,zs需要是二維數組。我可以用np.meshgrid轉換xsys但我該怎麼辦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的示例數據,以便您複製我的問題。每列分別由xsyszs組成。

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 

非常感謝您的任何幫助,您可以提供。

+1

'的cols = np.unique(XS).shape [0]'不會做你希望它,因爲你的'xs'什麼在不同的z平面上是不相同的。正如@moarningsun所提到的那樣,似乎有一個長度爲7的重複序列,但是'cols'將會是一些大於7的數字,碰巧不是'xs.size'的因素,因此是錯誤。 –

回答

2

other answer也適用於此,因爲您已經有一個(常規)網格。然而,採取獨特的價值觀不適用於您的數據(顯然)。相反,我查看了數據並手動決定了網格的形狀,即在我們返回x=0之前總是有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() 

證明:

the surface

+0

好東西!這已經完成了這個訣竅,我可以看到,使用你的扣除方法進行整形(對於這個數據集你可以得到7個入口)也適用於我的大得多的數據集。 – Timballisto

相關問題