Matplotlib - 3D 绘图



3D 绘图是一种以图形格式表示三维数据的方式。它允许您在三个空间维度(表示为 X、Y 和 Z 坐标)中可视化信息。在 3D 图中,数据点不仅位于平面中,而且具有深度,从而创建数据集更详细的表示。

3D Plotting

Matplotlib 中的 3D 绘图

在 Matplotlib 中,我们可以使用 **mpl_toolkits.mplot3d** 模块创建三维图。此模块提供用于创建三维可视化的工具,包括散点图、线图、表面图等。这些图提供了一种在三维空间中表示和探索数据点或数学函数的方式。您可以自定义颜色、标记、标签和透视等方面,以更有效地传达信息。

我们可以将 **numpy** 库与 **mpl_toolkits.mplot3d** 模块集成,以生成多维数据和不同的函数,例如 scatter、plot_surface 或 plot_wireframe。

mpl_toolkits.mplot3d 模块

Matplotlib 中的“mpl_toolkits.mplot3d”模块增强了库的三维绘图功能。它引入了“Axes3D”类,该类能够创建 3D 子图。此模块通过 scatter()(用于 3D 散点图)、plot_surface()(用于表面图)和 plot_wireframe()(用于线框表示)等函数,促进了三维数据可视化。

3D 散点图

Matplotlib 中的 3D 散点图是一种可视化,其中数据点在三维空间中表示为单个标记。每个数据点由三个值定义,分别对应于其沿 X、Y 和 Z 轴的位置。这些轴创建了一个三维网格,每个标记都放置在该空间中的指定坐标处。我们可以使用 scatter() 函数创建此类型的图。

示例

在以下示例中,我们使用 NumPy 生成随机 3D 数据点,并使用蓝色标记创建 3D 散点图。我们在三维空间中显示图,其中 x、y 和 z 轴表示点的坐标 -

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Generating random 3D data
np.random.seed(42)
n_points = 100
x = np.random.rand(n_points)
y = np.random.rand(n_points)
z = np.random.rand(n_points)

# Creating a 3D scatter plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='blue', marker='o')

ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('3D Scatter Plot')
plt.show()

输出

生成的图显示了曲线下逐渐的颜色过渡 -

3D Scatter Plot

3D 线图

Matplotlib 中的 3D 线图是一种图形表示,显示三维空间中一系列点之间的连接。与传统 2D 线图(其中点在平面上连接)不同,3D 线图扩展到三个维度,在 X、Y 和 Z 轴上形成一条连续的线。

我们可以使用 plot() 函数在 matplotlib 中创建 3D 线图。当我们将此函数与 projection='3d' 设置一起使用时,它能够生成 3D 线图。

示例

在这里,我们通过基于参数方程定义坐标(x、y 和 z)来生成 3D 线图的数据。生成的图在三维空间中显示螺旋形。x、y 和 z 轴分别表示坐标 -

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Generating data for a 3D line plot
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)

# Creating a 3D line plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z, label='3D Line Plot')

ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('3D Line Plot')
plt.show()

输出

执行上述代码后,我们将获得以下输出 -

3D Line Plot

3D 表面图

Matplotlib 中的 3D 表面图是三维空间中数学函数或数据集的视觉表示。此图不使用平面线或标记,而是使用连续的表面来显示变量如何在两个输入维度(X 和 Y)上变化,并取决于第三个维度(Z)。我们可以使用 plot_surface() 函数创建此类型的图。

示例

在这里,我们通过计算网格上每个点到原点的欧几里德距离的正弦来生成 3D 表面图的数据。生成的图可视化了一个基于正弦函数上升和下降的表面。x、y 和 z 轴分别表示坐标和表面的高度 -

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Generating data for a 3D surface plot
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))

# Creating a 3D surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, cmap='viridis')

ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('3D Surface Plot')
plt.show()

输出

执行上述代码后,我们将获得以下输出 -

3D Surface Plot

3D 条形图

Matplotlib 中的 3D 条形图是一种视觉表示,其中数据在三维空间中使用矩形条表示。与常规条形图(其中条形沿两个轴(X 和 Y)放置)类似,3D 条形图添加了第三个维度(Z)来表示每个条形的高度或幅度。我们可以使用 bar3d() 函数创建此类型的图。

示例

在下面的示例中,我们生成一个 3D 条形图的数据,该图在“x”和“y”方向上都有五个条形。每个条形的高度由“z”数组中的值确定。生成的图可视化了一组具有不同高度的三维条形,x、y 和 z 轴分别表示图的维度 -

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Generating data for a 3D bar plot
x_pos = np.arange(1, 6)
y_pos = np.arange(1, 6)
x_pos, y_pos = np.meshgrid(x_pos, y_pos)
z_pos = np.zeros_like(x_pos)
z = np.array([[5, 8, 3, 6, 2],
   [1, 2, 3, 4, 5],
   [2, 3, 6, 7, 8],
   [5, 6, 7, 8, 9],
   [3, 4, 5, 7, 8]])

# Creating a 3D bar plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.bar3d(x_pos.flatten(), y_pos.flatten(), z_pos.flatten(), 0.8, 0.8, z.flatten(), shade=True)

ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
ax.set_title('3D Bar Plot')
plt.show()

输出

执行上述代码后,我们将获得以下输出 -

3D Bar Plot
广告

© . All rights reserved.