95. Matplotlib 3D Plotting Methods#
95.1. Introduction#
Matplotlib is an open-source plotting library that supports the Python language. It is popular among various people such as Python engineers, scientific researchers, and data engineers because of its support for a rich variety of plot types, simple plotting methods, and comprehensive interface documentation. In this experiment, we will learn the methods and techniques of using Matplotlib for plotting.
95.2. Key Points#
3D Plotting
3D Mixed Plots
3D Subplots
95.3. 3D Plotting#
Previously, we have learned how to draw simple 2D images
using the
pyplot
module in Matplotlib. In fact, Matplotlib can also draw 3D
images. Different from 2D images, drawing 3D images is
mainly achieved through the
mplot3d
module. However, when using Matplotlib to draw 3D images, it
is actually displayed on a 2D canvas. Therefore, generally
when drawing 3D images, the
pyplot
module also needs to be imported.
The
mplot3d
module mainly contains 4 major categories, namely:
-
mpl_toolkits.mplot3d.axes3d()
-
mpl_toolkits.mplot3d.axis3d()
-
mpl_toolkits.mplot3d.art3d()
-
mpl_toolkits.mplot3d.proj3d()
Among them,
axes3d()
mainly contains various classes and methods for implementing
plotting.
axis3d()
mainly contains classes and methods related to the
coordinate axes.
art3d()
contains some classes and methods that can convert 2D images
and be used for 3D plotting.
proj3d()
contains some miscellaneous classes and methods, such as
calculating the length of 3D vectors, etc.
Generally, the class we use the most is
mpl_toolkits.mplot3d.axes3d.Axes3D()
under
mpl_toolkits.mplot3d.axes3d()
, and there are methods for drawing different types of 3D
graphs under
Axes3D()
.
Next, we will learn how to draw 3D graphs with Matplotlib through several examples. First, let’s draw a 3D scatter plot.
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib inline
# x, y, z 均为 100 个随机数
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
z = np.random.normal(0, 1, 100)
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x, y, z)
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x1178d97d0>
<Figure size 640x480 with 0 Axes>
The difference in data between 3D graphs and 2D graphs is that 3D graphs have an additional set of data for measuring the extra dimension.
When we draw 3D graphs in a desktop environment, we can arbitrarily drag the angle with the mouse, but this is not supported in the Jupyter Notebook environment, which only shows a static image of the default perspective of the 3D graph.
The line graph is similar to the scatter plot and requires the numerical values of the three coordinates \(x\), \(y\), and \(z\) to be passed in. The detailed code is as follows.
# 生成数据
x = np.linspace(-6 * np.pi, 6 * np.pi, 1000)
y = np.sin(x)
z = np.cos(x)
# 创建 3D 图形对象
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(x, y, z)
[<mpl_toolkits.mplot3d.art3d.Line3D at 0x117acab50>]
<Figure size 640x480 with 0 Axes>
After drawing the line graph, let’s continue to try drawing a 3D bar graph. In fact, its drawing steps are very similar to the above.
# 创建 3D 图形对象
fig = plt.figure()
ax = Axes3D(fig)
# 生成数据并绘图
x = [0, 1, 2, 3, 4, 5, 6]
for i in x:
y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
z = abs(np.random.normal(1, 10, 10))
ax.bar(y, z, i, zdir='y', color=['r', 'g', 'b', 'y'])
<Figure size 640x480 with 0 Axes>
Next, the 3D surface plot to be drawn is a bit more troublesome. We need to perform matrix processing on the data. In fact, it is very similar to drawing a 2D contour plot, except that one more dimension is added.
# 创建 3D 图形对象
fig = plt.figure()
ax = Axes3D(fig)
# 生成数据
X = np.arange(-2, 2, 0.1)
Y = np.arange(-2, 2, 0.1)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(X ** 2 + Y ** 2)
# 绘制曲面图,并使用 cmap 着色
ax.plot_surface(X, Y, Z, cmap=plt.cm.winter)
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x117b98310>
<Figure size 640x480 with 0 Axes>
cmap=plt.cm.winter
means that the
winter
color scheme is adopted. In addition to declaring a 3D graph
through
Axes3D()
, we can also declare a 3D graph through the
projection='3d'
parameter.
95.4. 3D Mixed Graph#
A mixed graph is to draw two different types of graphs on one graph. Generally, there are preconditions for drawing a mixed graph, that is, the ranges of the two different types of graphs are roughly the same. Otherwise, there will be serious proportion disharmony, making the mixed graph meaningless.
# 创建 3D 图形对象
fig = plt.figure()
ax = Axes3D(fig)
# 生成数据并绘制图 1
x1 = np.linspace(-3 * np.pi, 3 * np.pi, 500)
y1 = np.sin(x1)
ax.plot(x1, y1, zs=0, c='red')
# 生成数据并绘制图 2
x2 = np.random.normal(0, 1, 100)
y2 = np.random.normal(0, 1, 100)
z2 = np.random.normal(0, 1, 100)
ax.scatter(x2, y2, z2)
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x117d59fd0>
<Figure size 640x480 with 0 Axes>
95.5. 3D Subplot#
We have learned how to draw 2D subplots. In fact, the same principle applies in 3D. We can draw 2D images and 3D images together, or draw several 3D images together. Here, taking the line graph and surface graph drawn above as examples, let’s see what code needs to be added or deleted.
# 创建 1 张画布
fig = plt.figure(figsize=(8, 4))
# 向画布添加子图 1
ax1 = fig.add_subplot(1, 2, 1, projection='3d')
# 生成子图 1 数据
x = np.linspace(-6 * np.pi, 6 * np.pi, 1000)
y = np.sin(x)
z = np.cos(x)
# 绘制第 1 张图
ax1.plot(x, y, z)
# 向画布添加子图 2
ax2 = fig.add_subplot(1, 2, 2, projection='3d')
# 生成子图 2 数据
X = np.arange(-2, 2, 0.1)
Y = np.arange(-2, 2, 0.1)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(X ** 2 + Y ** 2)
# 绘制第 2 张图
ax2.plot_surface(X, Y, Z, cmap=plt.cm.winter)
The drawing of 3D graphics is actually a derivative of 2D graphics. There is not much difference in the drawing method. You need to organize appropriate data and declare a 3D drawing object.
95.6. Summary#
Through the study of this experiment, I believe you have initially mastered the methods and techniques of using Matplotlib to draw 3D graphs. Of course, if you are very interested in Matplotlib, you can also learn more about Matplotlib through the official documentation.