Matplotlib 简明教程

Matplotlib - 3D Contours

3D 轮廓线是指三维空间中描绘物体形状和高度的线或曲线。这些轮廓线帮助我们理解物体的不同部分有多高或多低。它们通常用于地理、工程和艺术等领域,以更详细的方式表示物体的形状。

例如,如果有一座山,它的 3D 轮廓线将从所有侧面展示坡道、峡谷和山峰。同样,如果有一个动物雕塑,它的 3D 轮廓线将从不同的视点描绘出它身体、头部和四肢的形状 -

3d contours1

3D Contours in Matplotlib

在 Matplotlib 中,3D 轮廓线表示三维物体的表面。它允许您通过提供表示 x、y 和 z 坐标的数据点创建 3D 等值线图。这些点定义了您想要可视化的物体的形状。然后,Matplotlib 可以生成轮廓线或曲面来表示 3D 数据的轮廓。

您可以使用“mpl_toolkits.mplot3d”模块中的 contour3D() 函数在 Matplotlib 中创建 3d 轮廓。此函数接受三个坐标 - X、Y 和 Z 作为数组,并在 X 和 Y 坐标上绘制一条线,以显示 3D 物体沿 z 轴的轮廓或高度变化。

让我们从绘制一个基本的 3D 轮廓开始。

Basic 3D Contour

Matplotlib 中的基本 3D 轮廓就像在 3D 中绘制地图上的等高线。它使用 X、Y 和 Z 轴显示曲面的高度变化。

Example

在下面的示例中,我们首先绘制 X 和 Y 坐标来创建一个基本的 3D 轮廓。然后,我们取 X 和 Y 的正弦和余弦值的总和来获取高度变化。结果图显示了在 Z 轴上具有不同高度的形状轮廓 -

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

# Creating data
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) + np.cos(Y)

# Creating a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plotting the 3D contour
ax.contour3D(X, Y, Z, 50, cmap='viridis')

# Customizing the plot
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.set_title('Basic 3D Contour Plot')

# Displaying the plot
plt.show()

以下是上面代码的输出: -

3d contours2

Parametric 3D Contours

Matplotlib 中的参数化 3D 轮廓使用三维中的数学参数表示不同高度的形状轮廓。轮廓线不仅由 X、Y 和 Z 坐标的变化定义,还由参数的变化定义。

Example

在这里,我们根据 3D 物体的尺寸 (®)、厚度 (r) 和初始坐标 (u, v) 对 X、Y 和 Z 坐标进行参数化。结果图创建了一个甜甜圈形状的 3D 轮廓 -

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

# Parametric equations for a torus
def torus_parametric(u, v, R=1, r=0.3):
   x = (R + r * np.cos(v)) * np.cos(u)
   y = (R + r * np.cos(v)) * np.sin(u)
   z = r * np.sin(v)
   return x, y, z

# Creating data
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, 2 * np.pi, 100)
U, V = np.meshgrid(u, v)
X, Y, Z = torus_parametric(U, V)

# Creating a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plotting the parametric 3D contour
ax.contour3D(X, Y, Z, 50, cmap='plasma')

# Customizing the plot
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.set_title('Parametric 3D Contour Plot (Torus)')

# Displaying the plot
plt.show()

执行上述代码,我们将得到以下输出 −

3d contours3

3D Contours from Irregular Data

在 Matplotlib 中,来自不规则数据的 3D 轮廓展示了数据点随机的 3D 曲面的轮廓。在这类轮廓中,我们通过基于 X、Y 和 Z 值估算值来计算缺失数据点。

Example

以下示例从不规则数据创建了一个 3D 轮廓。在这里,我们计算缺失数据点并使用已知数据点对它们进行线性插值。这创建了一个平滑且连续的 3D 轮廓,结果为 -

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

# Creating irregularly spaced data
np.random.seed(42)
x = np.random.rand(100)
y = np.random.rand(100)
z = np.sin(x * y)

# Creating a regular grid
xi, yi = np.linspace(x.min(), x.max(), 100), np.linspace(y.min(), y.max(), 100)
xi, yi = np.meshgrid(xi, yi)

# Combining irregular data onto the regular grid
zi = griddata((x, y), z, (xi, yi), method='linear')

# Creating a 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plotting the 3D contour from irregular data on the regular grid
ax.contour3D(xi, yi, zi, 50, cmap='viridis')

# Customizing the plot
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.set_title('3D Contour Plot from Irregular Data')

# Displaying the plot
plt.show()

执行上面的代码后,我们得到以下输出: -

3d contours4

Contour Lines in 3D Contours

在 Matplotlib 中,3D 轮廓中的等值线在三维中直观地表示物体的 3D 轮廓及其等值线。等值线表示 3D 轮廓的坡度,并在 XY 平面中表示,因为它们没有 Z 值(无深度)。“contour()”函数用于显示物体的等值线。

Example

现在,我们正在创建对象的 3D 轮廓和等值线。我们在 z 轴上绘制 3D 轮廓,在 XY 平面绘制等值线。结果图显示了 z 轴上物体的轮廓以及在 XY 平面上的其斜坡 -

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

# Creating data
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 plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plotting the 3D contour
ax.contour3D(X, Y, Z, 50, cmap='plasma')

# Adding contour lines on the XY plane
ax.contour(X, Y, Z, zdir='z', offset=np.min(Z), cmap='plasma')

# Customizing the plot
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.set_title('3D Contour Plot with Contour Lines')

# Displaying the plot
plt.show()

执行上述代码,我们将得到以下输出 −

3d contours5