Matplotlib 简明教程
Matplotlib - Manual Contour
总体而言,手动等高线绘制是指手动勾勒对象或特定区域边界的过程,而不是依赖于自动方法。通常这样做是为了创建图像中形状和边界的精确表述。
Manual contouring in general refers to the process of outlining the boundaries of an object or a specific area by hand rather than relying on automated methods. This is usually done to create accurate representations of the shapes and boundaries within the image.
等高线表示地图或图表上的一个恒定值。这就像在等高线地图上绘制一条线来连接具有相同高度的点,或在天气图上绘制一条线来连接具有相同温度的地点。
A contour line represents a constant value on a map or a graph. It is like drawing a line to connect points that share the same height on a topographic map or lines on a weather map connecting places with the same temperature.
Manual Contour in Matplotlib
在 Matplotlib 中,手动等高线图是一种使用等高线在二维表面上表示三维数据的方法。这些线连接数据集中的等值点,创建一个类似于地图的可视化连续数据。
In Matplotlib, a manual contour plot is a way to represent three-dimensional data on a two-dimensional surface using contour lines. These lines connect points of equal value in a dataset, creating a map-like visualization of continuous data.
对于手动等高线图,您指定要绘制线的等高线级别或值。然后,该绘图将显示这些等高线,每条等高线表示数据集中的一个特定值。
For a manual contour plot, you specify the contour levels or values at which the lines should be drawn. The plot then displays these contour lines, with each line representing a specific value in the dataset.
Basic Manual Contour Plot
在 Matplotlib 中创建基本手动等值线涉及绘制连接具有相同值点的线条,形成封闭回路或曲线,代表测量值的不同等级。此过程允许自定义和微调等值线,以准确表示给定的数据。
Creating a basic manual contours in Matplotlib involves drawing lines to connect points with the same value, forming closed loops or curves that represent distinct levels of the measured quantity. This process allows for customization and fine-tuning of the contour lines to accurately represent the given data.
虽然手动等值线会很耗时,但它提供了一定程度的控制和精确度,这在某些情况下可能是必要的,而在另一种情况下,自动化方法可能不足以胜任。
While manual contouring can be time-consuming, it provides a level of control and precision that may be necessary in certain situations where automated methods may not be adequate.
Example
在下面的示例中,我们将使用 Matplotlib 创建一个简单的等值线图。我们使用 contour() 函数使用手动指定的值生成正弦函数的等值线,并在 XY 平面中叠加它们 −
In the following example, we are creating a simple contour plot using Matplotlib. We are using the contour() function to generate contour lines of a sine function, with manually specified levels, overlaying them on the XY-plane −
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()
以下是上面代码的输出: -
Following is the output of the above code −
Manual Contour Plot with Labeling
在 Matplotlib 中,带有标注的手动等值线图使用带有等值线的二维平面上显示三维数据,并使用标注来标注特定等值线等级。
In Matplotlib, a manual contour plot with labeling represents three-dimensional data on a two-dimensional surface with contour lines, and annotate specific contour levels with labels.
此类型的图显示等值线以显示数据值相等区域,并在这些线中添加文本标注以指示相应的数据值。这些标注有助于识别数据集中的重要特征或值,使得更容易解释该图并了解数据的分布。
This type of plot displays contour lines to show regions of equal data value, and adds text labels to these lines to indicate the corresponding data values. The labels help to identify important features or values in the dataset, making it easier to interpret the plot and understand the distribution of the data.
Example
在此,我们使用 clabel() 函数创建带有等值线内联标注的等值线图 −
In here, we are creating a contour plot with inline labels to the contour lines using the clabel() function −
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()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −
Manual Contour Plot with Filled Regions
在 Matplotlib 中,带有填充区域的手动等值线图使用颜色来可视化表示数据集中不同的级别或值,这与仅显示等值线的传统等值线图不同。
In Matplotlib, a manual contour plot with filled regions use colors to visually represent different levels or values in the dataset, unlike traditional contour plots that only show contour lines.
每个填充区域对应于特定值范围,而颜色则指示这些值的强度或大小。
Each filled region corresponds to a specific range of values, with colors indicating the intensity or magnitude of those values.
Example
在下面的示例中,我们使用 contourf() 函数和 "RdYlBu" 颜色映射创建正弦函数的填充等值线图。然后,我们添加一个颜色条以指示填充等值线的强度比例 −
In the example below, we are creating a filled contour plot of a sine function using the contourf() function with the "RdYlBu" colormap. We then add a color bar to indicate the intensity scale of the filled contours −
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()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
Manual Contour Plot with Custom Line Styles
在 Matplotlib 中,带有自定义线样式的手动等值线图是一种表示等值线的方法,其中每条线可以具有不同的可视化样式。
In Matplotlib, a manual contour plot with custom line styles is a way to represent contour lines, where each line can have a distinct visual style.
通常,等值线绘制为实线,但是通过自定义线样式,您可以修改线宽、颜色和图案等方面,以区分数据集中的不同等级或值。例如,您可以使用虚线、点线或较粗的线来突出显示特定的等值线等级或目标区域。
Generally, contour lines are drawn as solid lines, but with custom line styles, you can modify aspects like the line width, color, and pattern to distinguish different levels or values in the dataset. For example, you might use dashed lines, dotted lines, or thicker lines to highlight specific contour levels or regions of interest.
Example
现在,我们通过指定用于不同等级的一系列线样式来创建带有自定义线样式的手动等值线图。我们通过将 "linestyles" 参数传递给 contour() 函数来实现此目的 −
Now, we are creating a manual contour plot with custom line styles by specifying a list of line styles to use for different levels. We achieve this by passing the "linestyles" parameter to the contour() function −
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()
执行上述代码,我们将得到以下输出 −
On executing the above code we will get the following output −