Matplotlib 简明教程
Matplotlib - 3D Plotting
3D 绘制是一种以图形格式表示三维数据的途径。它允许您可视化三个空间维度中的信息,表示为 X、Y 和 Z 坐标。在 3D 绘制中,数据点不仅位于平面中,还有深度,可创建数据集的更详细表示。
A 3D plotting is a way to represent three dimensional data in a graphical format. It allows you to visualize the information in three spatial dimensions, represented as X, Y, and Z coordinates. In 3D plots, data points are not only located on a flat plane but also have depth, creating a more detailed representation of the dataset.
3D Plotting in Matplotlib
在 Matplotlib 中,我们可以使用 mpl_toolkits.mplot3d 模块创建三维绘图。该模块提供了创建三维可视化的工具,包括散点图、折线图、曲面图等。这些绘图提供了表示和探索三维空间中的数据点或数学函数的方法。您可以自定义颜色、标记、标签和透视等方面,以更有效地传达信息。
In Matplotlib, we can create a three-dimensional plot using the mpl_toolkits.mplot3d module. This module provides tools to create three-dimensional visualizations, including scatter plots, line plots, surface plots, and more. These plots provide a way to represent and explore data points or mathematical functions in three-dimensional space. You can customize aspects such as color, markers, labels, and perspective to convey information more effectively.
我们可以将 numpy 库与 mpl_toolkits.mplot3d 模块集成起来生成多维数据,以及不同的函数,如散点、plot_surface 或 plot_wireframe。
We can integrate the numpy library with the mpl_toolkits.mplot3d module to generate multidimensional data, and different functions, such as scatter, plot_surface, or plot_wireframe.
The mpl_toolkits.mplot3d Module
Matplotlib 中的 “mpl_toolkits.mplot3d” 模块增强了该库的三维绘图功能。它引入了 “Axes3D” 类,该类允许创建 3D 子绘图。该模块通过 scatter()(用于 3D 散点图)、plot_surface()(用于曲面图)和 plot_wireframe()(用于线框表示)等函数,促进了三维中数据的可视化。
The "mpl_toolkits.mplot3d" module in Matplotlib enhances the library’s capabilities for three-dimensional plotting. It introduces the "Axes3D" class, which enables the creation of 3D subplots. This module facilitates the visualization of data in three dimensions through functions such as scatter() for 3D scatter plots, plot_surface() for surface plots, and plot_wireframe() for wireframe representations.
3D Scatter Plot
Matplotlib 中的 3D 散点图是一种可视化形式,其中数据点在三维空间中表示为单独标记。每个数据点都由三个值定义,对应于其沿 X、Y 和 Z 轴的位置。这些轴创建了一个三维网格,每个标记在这个空间中放置在指定坐标处。我们可以使用 scatter() 函数创建这种类型的绘图。
A 3D scatter plot in Matplotlib is a visualization where data points are represented as individual markers in a three-dimensional space. Each data point is defined by three values, corresponding to its positions along the X, Y, and Z axes. These axes create a three-dimensional grid, and each marker is placed at the specified coordinates in this space. We can create this type of plot using the scatter() function.
Example
在以下示例中,我们使用 NumPy 生成随机 3D 数据点,并使用蓝色标记创建一个 3D 散点图。我们在一个三维空间中显示该绘图,其中 x、y 和 z 轴表示点的坐标 −
In the following example, we are generating random 3D data points using NumPy and creating a 3D scatter plot with blue markers. We display the plot in a three-dimensional space, where the x, y, and z axes represent the coordinates of the points −
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()
由此产生的绘图显示曲线上面的逐渐变色 −
The resulting plot shows a gradual color transition under the curve −
3D Line Plot
Matplotlib 中的 3D 折线图是一种图形表示,它显示了三维空间中一系列点之间的连接。与平面中连接点的传统 2D 折线图不同,3D 折线图延伸到三个维度,在 X、Y 和 Z 轴中形成一条连续线。
A 3D line plot in Matplotlib is a graphical representation that shows the connection between a sequence of points in a three-dimensional space. Unlike traditional 2D line plots where points are connected on a flat plane, a 3D line plot extends into three dimensions, forming a continuous line in the X, Y, and Z axes.
我们可以使用 plot() 函数在 matplotlib 中创建 3D 折线图。当我们将此函数与 projection='3d' 设置结合使用时,它可以生成 3D 折线图。
We can create 3D line plot in matplotlib using the plot() function. When we use this function in conjunction with the projection='3d' setting, it enables the generation of 3D line plots.
Example
在这里,我们通过基于参数方程定义坐标 (x、y 和 z) 来生成 3D 折线图的数据。由此产生的绘图在三维空间中显示一个螺旋形状。x、y 和 z 轴表示各自的坐标 −
In here, we are generating data for a 3D line plot by defining coordinates (x, y, and z) based on a parameterized equation. The resulting plot displays a helical shape in three-dimensional space. The x, y, and z axes represent the respective coordinates −
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()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
3D Surface Plot
Matplotlib 中的 3D 曲面图是对三维空间中数学函数或数据集的可视化表示。该绘图不使用平面线或标记,而是使用一个连续曲面,显示变量如何在两个输入维度 (X 和 Y) 中变化,并依赖于第三个维度 (Z)。我们可以使用 plot_surface() 函数创建这种类型的绘图。
A 3D surface plot in Matplotlib is a visual representation of a mathematical function or a dataset in three-dimensional space. Instead of using flat lines or markers, this plot uses a continuous surface to show how a variable changes across two input dimensions (X and Y) and is dependent on a third dimension (Z). We can create this type of plot using the plot_surface() function.
Example
在这里,我们通过计算网格上每个点的欧几里德距离的正弦来生成用于 3D 曲面图的数据。由此产生的绘图可视化了一个根据正弦函数上下升降的曲面。x、y 和 z 轴表示坐标和曲面的高度 −
In here, we are generating data for a 3D surface plot by calculating the sine of the Euclidean distance from the origin for each point on a grid. The resulting plot visualizes a surface that rises and falls based on the sine function. The x, y, and z axes represent the coordinates and the height of the surface −
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()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
3D Bar Plot
Matplotlib 中的 3D 条形图是一种可视化表示,其中数据使用三维空间中的矩形条形来表示。与沿着两个轴 (X 和 Y) 放置条形的常规条形图类似,3D 条形图添加了第三个维度 (Z) 来表示每个条形的高度或大小。我们可以使用 bar3d() 函数创建这种类型的绘图。
A 3D bar plot in Matplotlib is a visual representation where data is presented using rectangular bars in three-dimensional space. Similar to a regular bar plot where bars are positioned along two axes (X and Y), a 3D bar plot adds a third dimension (Z) to represent the height or magnitude of each bar. We can create this type of plot using the bar3d() function.
Example
在下面的示例中,我们生成具有 x 和 y 方向上五个条形的 3D 条形图的数据。每个条形的高度由 z 数组中的值决定。由此产生的绘图可视化了一组高度不同的三维条形,x、y 和 z 轴表示绘图的维度 −
In the example below, we are generating data for a 3D bar plot with five bars in both "x" and "y" directions. The height of each bar is determined by the values in the "z" array. The resulting plot visualizes a set of three-dimensional bars with varying heights, and the x, y, and z axes represent the dimensions of the plot −
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()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −