Matplotlib 简明教程

Matplotlib - 3D Plotting

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

3d plotting1

3D Plotting in Matplotlib

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

我们可以将 numpy 库与 mpl_toolkits.mplot3d 模块集成起来生成多维数据,以及不同的函数,如散点、plot_surface 或 plot_wireframe。

The mpl_toolkits.mplot3d Module

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

3D Scatter Plot

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

Example

在以下示例中,我们使用 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 plotting2

3D Line Plot

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

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

Example

在这里,我们通过基于参数方程定义坐标 (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 plotting3

3D Surface Plot

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

Example

在这里,我们通过计算网格上每个点的欧几里德距离的正弦来生成用于 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 plotting4

3D Bar Plot

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

Example

在下面的示例中,我们生成具有 x 和 y 方向上五个条形的 3D 条形图的数据。每个条形的高度由 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 plotting5