gpt4 book ai didi

python - 轮廓和轮廓f的使用

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:44 24 4
gpt4 key购买 nike

我有一个要点列表:

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

但我有一个异常(exception):

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 都必须是 2D数组。

如果您的数据不在网格上,您要么需要将其插值到网格中,要么无法使用等值线。

一个简单的替代方法是使用 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 .

您还可以查看:

关于python - 轮廓和轮廓f的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43395958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com