2017-04-13 116 views
1

的我點的列表:使用輪廓和contourf

pointList = [ [x1,y1,z1], [x2,y2,z2], ... [xn,yn,zn]] 

,我要畫這組點的等高線圖。

我嘗試:

import matplotlib.pyplot as plt 
pointList = [ [x1,y1,z1], [x2,y2,z2], ... [xn,yn,zn]] 
x = [el[0] for el in pointList] 
y = [el[1] for el in pointList] 
z = [el[2] for el in pointList] 
plt.contourf(x,y,z) 
plt.show() 

,但我有這樣的例外:

TypeError: Input z must be a 2D array. 

這很奇怪,因爲matplotlib,我發現文檔中:

Call signatures: 
contour(Z) 
make a contour plot of an array Z. The level values are chosen automatically. 
contour(X,Y,Z) 
X, Y specify the (x, y) coordinates of the surface 

所以我不不明白爲什麼它會失敗...

+0

你的觀點是否確實創造了一個表面? 'X'和'Y'是用於表面的唯一'x'和'y'值。它不打算用於分散數據 – Suever

+0

不確定要理解。我應該怎麼做才能「實際」創造一個表面? – rudy

+4

可能重複的[散射輪廓](http://stackoverflow.com/questions/18764814/make-contour-of-scatter) – Suever

回答

1

在任一情況下,contour(Z)contour(X,Y,Z),輸入Z必須是二維數組。

如果您的數據不在網格中,您需要將其插入網格或不能使用輪廓。

一個簡單的選擇是使用tricontour

import matplotlib.pyplot as plt 
import numpy as np 
pointList = [ [x1,y1,z1], [x2,y2,z2], ... [xn,yn,zn]] 
pointList = np.array(pointList) 
plt.tricontour(pointList[:,0],pointList[:,1],pointList[:,2]) 
plt.show() 

有其與經內插的數據的比較contourtricontour一個很好的例子:tricontour_vs_griddata

你也可以看: