Matplotlib 简明教程

Matplotlib - Manual Contour

总体而言,手动等高线绘制是指手动勾勒对象或特定区域边界的过程,而不是依赖于自动方法。通常这样做是为了创建图像中形状和边界的精确表述。

等高线表示地图或图表上的一个恒定值。这就像在等高线地图上绘制一条线来连接具有相同高度的点,或在天气图上绘制一条线来连接具有相同温度的地点。

manual contour1

Manual Contour in Matplotlib

在 Matplotlib 中,手动等高线图是一种使用等高线在二维表面上表示三维数据的方法。这些线连接数据集中的等值点,创建一个类似于地图的可视化连续数据。

对于手动等高线图,您指定要绘制线的等高线级别或值。然后,该绘图将显示这些等高线,每条等高线表示数据集中的一个特定值。

Basic Manual Contour Plot

在 Matplotlib 中创建基本手动等值线涉及绘制连接具有相同值点的线条,形成封闭回路或曲线,代表测量值的不同等级。此过程允许自定义和微调等值线,以准确表示给定的数据。

虽然手动等值线会很耗时,但它提供了一定程度的控制和精确度,这在某些情况下可能是必要的,而在另一种情况下,自动化方法可能不足以胜任。

Example

在下面的示例中,我们将使用 Matplotlib 创建一个简单的等值线图。我们使用 contour() 函数使用手动指定的值生成正弦函数的等值线,并在 XY 平面中叠加它们 −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
# Creating a grid of x and y values
X, Y = np.meshgrid(x, y)
# Calculating the value of Z using the given function
Z = np.sin(X**2 + Y**2)

# Creating contour plot with manual levels
levels = [-0.9, -0.6, -0.3, 0, 0.3, 0.6, 0.9]
plt.contour(X, Y, Z, levels=levels)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Manual Contour Plot')
# Displaying the plot
plt.show()

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

manual contour2

Manual Contour Plot with Labeling

在 Matplotlib 中,带有标注的手动等值线图使用带有等值线的二维平面上显示三维数据,并使用标注来标注特定等值线等级。

此类型的图显示等值线以显示数据值相等区域,并在这些线中添加文本标注以指示相应的数据值。这些标注有助于识别数据集中的重要特征或值,使得更容易解释该图并了解数据的分布。

Example

在此,我们使用 clabel() 函数创建带有等值线内联标注的等值线图 −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X**2 + Y**2)

# Contour plot with labels
levels = [-0.9, -0.6, -0.3, 0, 0.3, 0.6, 0.9]
CS = plt.contour(X, Y, Z, levels=levels)
plt.clabel(CS, inline=True, fontsize=12)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Manual Contour Plot with Labeling')
plt.show()

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

manual contour3

Manual Contour Plot with Filled Regions

在 Matplotlib 中,带有填充区域的手动等值线图使用颜色来可视化表示数据集中不同的级别或值,这与仅显示等值线的传统等值线图不同。

每个填充区域对应于特定值范围,而颜色则指示这些值的强度或大小。

Example

在下面的示例中,我们使用 contourf() 函数和 "RdYlBu" 颜色映射创建正弦函数的填充等值线图。然后,我们添加一个颜色条以指示填充等值线的强度比例 −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X**2 + Y**2)

# Creating contour plot with filled regions
plt.contourf(X, Y, Z, levels=[-0.9, -0.6, -0.3, 0, 0.3, 0.6, 0.9], cmap='RdYlBu')
plt.colorbar()
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Manual Contour Plot with Filled Regions')
plt.show()

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

manual contour4

Manual Contour Plot with Custom Line Styles

在 Matplotlib 中,带有自定义线样式的手动等值线图是一种表示等值线的方法,其中每条线可以具有不同的可视化样式。

通常,等值线绘制为实线,但是通过自定义线样式,您可以修改线宽、颜色和图案等方面,以区分数据集中的不同等级或值。例如,您可以使用虚线、点线或较粗的线来突出显示特定的等值线等级或目标区域。

Example

现在,我们通过指定用于不同等级的一系列线样式来创建带有自定义线样式的手动等值线图。我们通过将 "linestyles" 参数传递给 contour() 函数来实现此目的 −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(-3, 3, 100)
y = np.linspace(-3, 3, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X**2 + Y**2)

# Contour plot with custom line styles
levels = [-0.9, -0.6, -0.3, 0, 0.3, 0.6, 0.9]
plt.contour(X, Y, Z, levels=levels, linestyles=['-', '--', '-.', ':', '-', '--'])
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Manual Contour Plot with Customized Line Styles')
plt.show()

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

manual contour5