您可以使用 matplotlib.pyplot.tricontourf 进行操作,但这取决于您存储数据的方式。
你应该有 4 个数组:
-
x 数组 (1, N) 带 x 坐标
-
y 数组 (1, N) 带 y 坐标
-
z 数组 (1, N) 带 z 值
-
triangles x 和 y 点的索引列表,它们是剪切三角形的顶点
x = np.asarray([8, 10, 8, 14, 9, 11, 13, 10, 12, 11])
y = np.asarray([2, 2, 2, 2, 2.56666667, 2.56666667, 2.56666667, 3.13333333, 3.13333333, 3.7])
z = np.cos(2.5*x*x)*np.cos(1.5*y*x)
triangles = [[0, 3, 9]]
然后您可以执行三角测量:
triang = mtri.Triangulation(x, y, triangles)
完整代码
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
import numpy as np
x = np.asarray([8, 10, 8, 14, 9, 11, 13, 10, 12, 11])
y = np.asarray([2, 2, 2, 2, 2.56666667, 2.56666667, 2.56666667, 3.13333333, 3.13333333, 3.7])
z = np.cos(2.5*x*x)*np.cos(1.5*y*x)
triangles = [[0, 3, 9]]
triang = mtri.Triangulation(x, y, triangles)
fig, ax = plt.subplots()
t = ax.tricontourf(triang, z)
plt.colorbar(t)
plt.show()
如果您的数据不是这种形状,您应该重新调整您的数组,以便如上所述获得x、y 和z,并计算triangles 索引列表。如果您需要帮助,请提供您的数据。