Matplotlib 简明教程

Matplotlib - Contour Plots

等高线图(也称为等高线地图或水平图)是二维平面上的三维曲面的图形表示。

A contour plot, also known as a contour map or a level plot, is a graphical representation of a three-dimensional surface on a two-dimensional plane.

在等高线图中,曲面由一系列等高线表示。每条等高线都连接曲面上具有相等值的点,显示函数具有相同值的位置。这些等高线以恒定的间隔或“水平”绘制,因此名称为“水平图”。

In a contour plot, the surface is represented by a series of contour lines. Each contour line connects points of equal value on the surface, showing regions where the function has the same value. These contour lines are drawn at constant intervals or "levels", hence the name "level plot".

试想你有一幅地图上的温度等高线图。每条等高线都表示具有相同温度的区域,例如 50°F、60°F 等。通过查看该图,你可以轻松地在整个地图上看出哪里更热或更冷。

Imagine you have a contour plot of temperature across a map. Each contour line represents areas with the same temperature, like 50°F, 60°F, and so on. By looking at the plot, you can easily see where it is hotter or cooler across the map −

contours plots1

Contour Plot in Matplotlib

你可以在 Matplotlib 中使用“matplotlib.pyplot”模块中的 contour() 函数创建等高线图。此函数接受 X 和 Y 坐标作为 1D 或 2D 数组,分别代表函数“Z”在其中计算的网格。“Z”是 2D 数组,其中包含对应于由 X 和 Y 定义的网格点的函数值。

You can create contour plots in Matplotlib using the contour() function in the "matplotlib.pyplot" module. This function accepts X and Y coordinates as either 1D or 2D arrays, representing the grid on which the function "Z" is evaluated. "Z" is a 2D array containing the function values corresponding to the grid points defined by X and Y.

让我们从绘制一个基本的等值线图开始。

Let’s start by drawing a basic contour plot.

Basic Contour Plot

Matplotlib 中的一个基本 3D 等值线图显示了连接相等值点的等值线,表示数据的层级或“高度”。每条等值线对应一个具体的值,形成了数据集的地图式表示。

A basic 3D contour in Matplotlib shows contour lines that connect points of equal value, representing the levels or "heights" of the data. Each contour line corresponds to a specific value, forming a map-like representation of the dataset.

在以下示例中,我们创建了一个基本的等值线图。我们为网格定义 x 和 y 坐标,然后使用一个数学函数生成 z 值。使用这些 x、y 和 z 值,我们使用 contour() 函数创建一个等值线图-

In the following example, we are create a basic contour plot. We define the x and y coordinates for the grid, and then use a mathematical function to generate the z values. With these x, y, and z values, we create a contour plot using the contour() function −

import matplotlib.pyplot as plt
import numpy as np

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

# Creating contour plot
plt.contour(X, Y, Z)

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic Contour Plot')

# Displaying the plot
plt.show()

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

Following is the output of the above code −

contours plots2

Filled Contour Plot

在 Matplotlib 中的填充等值线图中,它不仅显示等值线,还用颜色填充线之间的区域,创建数据表面的阴影表示。每种颜色表示数据的不同层级或“高度”,让你能够轻松地查看数据集中的分布。

In a filled contour plot in Matplotlib, instead of just showing contour lines, it fills in the areas between the lines with colors, creating a shaded representation of the data surface. Each color represents a different level or "height" of the data, allowing you to easily see the distribution within the dataset.

在这里,我们使用 contourf() 函数创建一个填充等值线图,它着色等值线之间的区域-

In here, we create a filled contour plot using the contourf() function, which colors the regions between contour lines −

import matplotlib.pyplot as plt
import numpy as np

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

# Creating a filled contour plot
plt.contourf(X, Y, Z)

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Filled Contour Plot')

# Displaying the plot
plt.show()

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

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

contours plots3

Contour Plot with Specific Levels

在具有特定层级的等值线图中,你需要指定绘制等值线的层级。每条等值线连接相等值点的,表示数据的不同层级或“高度”。这可定制可视化效果,以突出显示数据集中的特定特征或区间。

In a contour plot with specific levels, you specify the levels at which you want the contours to be drawn. Each contour line connects points of equal value, representing different levels or "heights" of the data. This allows you to customize the visualization to highlight specific features or intervals within the dataset.

在这个示例中,我们使用 Matplotlib 指定特定的等值线层级来定制等值线图。生成数据并创建等值线图后,我们使用 contour() 函数中的 levels 参数定义等值线层级-

In this example, we customize the contour plot by specifying specific contour levels using Matplotlib. After generating the data and creating the contour plot, we use the levels parameter in the contour() function to define the contour levels −

import matplotlib.pyplot as plt
import numpy as np

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

# Defining contour levels
levels = np.linspace(-1, 1, 20)

# Creating contour plot with specific levels
plt.contour(X, Y, Z, levels=levels)

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Contour Plot with Specific Levels')

# Displaying the plot
plt.show()

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

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

contours plots4

Contour Plot with Colorbar

在 Matplotlib 中,带有颜色块的等值线图显示等值线以显示数据集中相等值的点,并在绘图旁边显示一个颜色块以指示颜色与数据值之间的对应关系。颜色块充当视觉指南,帮助你了解绘图中不同颜色表示的数据值的范围和分布。

In Matplotlib, a contour plot with a colorbar displays contour lines to show points of equal value in the dataset, and a colorbar alongside the plot to indicate the correspondence between colors and data values. The colorbar acts as a visual guide, helping you to understand the range and distribution of data values represented by different colors in the plot.

在这里,我们使用 Matplotlib 创建一个带有颜色块的等值线图。生成数据并创建等值线图后,我们使用 colorbar() 函数为绘图添加一个颜色块。此颜色块提供了等值线图中对应的 z 值的视觉表示-

Here, we create a contour plot with a colorbar using Matplotlib. After generating the data and creating the contour plot, we add a colorbar to the plot using the colorbar() function. This colorbar provides a visual representation of the z values corresponding to the contour plot −

import matplotlib.pyplot as plt
import numpy as np

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

# Creating a contour plot
contour = plt.contour(X, Y, Z)

# Adding colorbar
plt.colorbar(contour, label='Z-values')

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Contour Plot with Colorbar')

# Displaying the plot
plt.show()

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

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

contours plots5