【问题标题】:use of contour and contourf使用contour和contourf
【发布时间】:2017-04-14 11:03:01
【问题描述】:

我有一个点列表:

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

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

【问题讨论】:

  • 你的点真的创造了一个表面吗? XY 是用于表面的唯一 xy 值。它不适用于分散的数据
  • 不明白。我应该如何“实际”创建一个曲面?
  • Make contour of scatter的可能重复
  • 你有分散的数据(看起来),所以你需要使用副本中的方法将其转换为常规网格

标签: python matplotlib contour


【解决方案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()

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

你也可以看看:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 2014-05-29
    • 2022-12-15
    • 1970-01-01
    相关资源
    最近更新 更多