Matplotlib - 手动等值线



手动等值线通常指手工勾勒物体或特定区域边界,而不是依赖自动化方法的过程。这通常是为了创建图像中形状和边界的精确表示。

等值线在图或地图上表示恒定值。它就像在地形图上连接具有相同高度的点,或在天气图上连接具有相同温度的地点。

Manual Contour

Matplotlib 中的手动等值线

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

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

基本的 手动等值线图

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

虽然手动等值线绘制可能非常耗时,但在某些情况下,自动化方法可能不足的情况下,它可以提供必要的控制和精度。

示例

在下面的例子中,我们使用 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()

输出

以下是上述代码的输出 -

Basic Manual Contour

带有标签的手动等值线图

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

这种类型的图显示等值线以显示数据值相等的区域,并向这些线添加文本标签以指示相应的数据值。标签有助于识别数据集中重要的特征或值,从而更容易解释该图并理解数据的分布。

示例

在这里,我们使用 `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 Contour Plot with Labeling

带有填充区域的手动等值线图

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

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

示例

在下面的示例中,我们使用 `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 Contour Plot with Filled Regions

带有自定义线型的手动等值线图

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

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

示例

现在,我们通过指定用于不同级别的线型列表来创建具有自定义线型的手动等值线图。我们通过将 "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 Contour with Custom Line Styles
广告
© . All rights reserved.