Matplotlib 简明教程

Matplotlib - 3D Contours

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

3D contours refer to the lines or curves that show the shape and height of objects in a three-dimensional space. These contours help us understand how high or low different parts of an object are. They are commonly used in fields like geography, engineering, and art to represent the shape of things in a more detailed way.

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

For example, if you have a mountain, its 3D contours will show the slopes, valleys, and peaks from all sides. Similarly, if you have a sculpture of an animal, its 3D contours will describe the shape of its body, head, and limbs from various viewpoints −

3d contours1

3D Contours in Matplotlib

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

In Matplotlib, 3D contours represent the surface of a three-dimensional object. It allows you to create 3D contour plots by providing your data points representing the x, y, and z coordinates. These points define the shape of the object you want to visualize. Then, Matplotlib can generate contour lines or surfaces to represent the contours of your 3D data.

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

You can create 3d contours in Matplotlib using the contour3D() function in the "mpl_toolkits.mplot3d" module. This function accepts the three coordinates - X, Y, and Z as arrays and plots a line across the X and Y coordinate to show the outline or change in height of a 3D object along the z-axis.

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

Let’s start by drawing a basic 3D contour.

Basic 3D Contour

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

A basic 3D contour in Matplotlib is like drawing elevation lines on a map but in three dimensions. It uses X, Y, and Z axes to show the changes in height of a surface across different points.

Example

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

In the following example, we are creating a basic 3D contour by first plotting the X and Y coordinates. Then, we take the sum of sine and cosine values of X and Y to get the change in elevation. The resultant plot displays the outline of the shape with different heights on Z axis −

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()

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

Following is the output of the above code −

3d contours2

Parametric 3D Contours

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

Parametric 3D contours in Matplotlib represent outline of shapes at different heights using mathematical parameters in three dimensions. The contours are not only defined by the changes in X, Y, and Z coordinates but also by changes in the parameters.

Example

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

In here, we are parameterizing the X, Y, and Z coordinates based on the the size ®, thickeness (r) and initial coordinates (u, v) of the 3D object. The resultant plot creates a donut-shaped 3D contour −

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()

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

On executing the above code we will get the following output −

3d contours3

3D Contours from Irregular Data

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

In Matplotlib, 3D contours from irregular data show the outline of a 3D surface whose data points are random. In this type of contour, we calculate the missing data points by estimating the values based on the X, Y, and Z values.

Example

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

The following example creates a 3D contour from irregular data. Here, we calculate the missing data points and interpolate them linearly with known data points. This creates a smooth and continuous 3D contour as the result −

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()

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

After executing the above code, we get the following output −

3d contours4

Contour Lines in 3D Contours

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

In Matplotlib, contour lines in 3D contours visually represents the 3D contour of an object along with its contour lines in three dimensions. The contour lines represent the slope of a 3D contour and are represented in the XY plane as they do not have any Z value (no depth). The function "contour()" is used to show the contour lines of an object.

Example

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

Now, we are creating a 3D contour and contour lines of an object. We plot a 3D contour on the z-axis and the contour lines on the XY plane. The resultant plot shows the outline of the object on the z-axis along with its slope on XY plane −

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()

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

On executing the above code we will get the following output −

3d contours5