2017-04-13 117 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

你也可以看: