轮廓和轮廓的使用

use of contour and 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

所以我不明白为什么会失败...

在任一情况下,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()

有一个很好的例子,它将 tricontourcontour 的插值数据进行比较:tricontour_vs_griddata.

您还可以看看:

  • Plotting Isolines/contours in matplotlib from (x, y, z) data set(对于三角轮廓和插值轮廓都有一个简单的例子)
  • matplotlib contour/contourf of **concave** non-gridded data(如果你的数据是凹的)