从matplotlib中的元组列表中绘制3D表面?

要从 matplotlib 中的元组列表绘制 3D 表面,我们可以采取以下步骤。

步骤

  • 设置图形大小并调整子图之间和周围的填充。

  • 制作一个元组列表。

  • 从元组列表中获取x、yz数据点。

  • 从坐标向量返回坐标矩阵。

  • 获取曲面图的h个数据点。

  • 创建新地物或激活现有地物。

  • 获取图形的当前轴 3d。

  • 创建曲面图。

  • 要显示图形,请使用Show()方法。

示例

import numpy as np
from matplotlib import pyplot as plt

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# List of tuples
data = [(1, 3, 2), (3, 5, 2), (4, 7, 4), (8, 7, 4),
        (3, 6, 1), (3, 9, 0), (3, 9, 0)]

# Data points from the list of tuples
x, y, z = zip(*data)

x, y = np.meshgrid(x, y)

h = x ** 2 + y ** 2

fig = plt.figure()

# Get the current axis
ax = fig.gca(projection='3d')

# Surface plot
ax.plot_surface(x, y, h, cmap='plasma')

plt.show()
输出结果

它将产生以下输出 -