在这篇文章中,我们将学习如何在Python中使用contour函数和Matpotlib创建等高线图。我们将研究不同类型的绘图函数和通过它们创建的不同类型的图。我们还将看一下代码,同时详细解释如何与之配合。
什么是等高线图?
空心等高线图
等高线是三维表面的二维表示,有曲线和接点。它是通过使用一个等高线函数(Z)来绘制的,该函数是两个变量(X,Y)的函数。
为了处理等高线图,我们需要两个库 - Matplotlib和NumPy。让我们来安装它们。
Matplotlib是一个基于Python的绘图库,用于创建图表和绘图。要安装Matplotlib,请输入以下命令。
pip install matplotlib
我们将需要另一个库--Python Numpy来创建我们的等高线图。要安装它,请键入以下命令。
pip install numpy
创建一个等高线图
有了基本的要求,让我们马上开始绘制我们的等高线图。
导入重要的库。
import matplotlib.pyplot as plt
import numpy as nump
初始化X、Y变量
在下面的代码中,变量X和Y被初始化为绘图的3维坐标。
element_ofx = nump.arange(0, 25, 4)
element_ofy = nump.arange(0, 26, 4)
用这两个变量创建等高线函数Z
[grid_ofX, grid_ofY] = nump.meshgrid(element_ofx, element_ofy)
fig, holowplt= plt.subplots(1, 1)
grid_ofZ = nump.cos(grid_ofX / 1) - nump.sin(grid_ofY / 2)
绘制等高线图
holowplt.contour(grid_ofX, grid_ofY, grid_ofZ)
holowplt.set_title('Contour Plot')
holowplt.set_xlabel('features of x-axis')
holowplt.set_ylabel('features of y-axis')
plt.show()
下面的代码演示了如何创建简单、空洞的matplotlib等高线图。
import matplotlib.pyplot as plt
import numpy as nump
element_ofx = nump.arange(0, 25, 4)
element_ofy = nump.arange(0, 26, 4)
# This numpy function creates 2-dimensional grid
[grid_ofX, grid_ofY] = nump.meshgrid(element_ofx, element_ofy)
# plots 2 graphs in one chart
fig, holowplt = plt.subplots(1, 1)
# Mathematical function for contour
grid_ofZ = nump.cos(grid_ofX / 1) - nump.sin(grid_ofY / 2)
# plots contour lines
holowplt.contour(grid_ofX, grid_ofY, grid_ofZ)
holowplt.set_title('Contour Plot')
holowplt.set_xlabel('features of x-axis')
holowplt.set_ylabel('features of y-axis')
plt.show()
输出:

空心等高线图
填充的等高线图
在这个例子中,我们将创建填充等高线图,而不是空心的。为了创建填充图,我们将使用'contourf'函数。整个程序与前面的例子非常相似,但有一些轻微的变化。
绘制等高线图
fillplot.contourf(grid_ofX, grid_ofY, grid_ofZ)
fillplot.set_title('Contour Plot')
fillplot.set_xlabel('features of x-axis')
fillplot.set_ylabel('features of y-axis')
让我们看一下整个代码,以便更好地理解。
import matplotlib.pyplot as plt
import numpy as nump
element_ofx = nump.arange(0, 25, 4)
element_ofy = nump.arange(0, 26, 4)
# This numpy function creates 2-dimensional grid
[grid_ofX, grid_ofY] = nump.meshgrid(element_ofx, element_ofy)
# plots 2 graphs in one chart
fig, fillplot = plt.subplots(1, 1)
# Mathematical function for contour
grid_ofZ = nump.cos(grid_ofX / 1) - nump.sin(grid_ofY / 2)
# plots contour lines
fillplot.contourf(grid_ofX, grid_ofY, grid_ofZ)
fillplot.set_title('Contour Plot')
fillplot.set_xlabel('features of x-axis')
fillplot.set_ylabel('features of y-axis')
plt.show()
输出:

填充的等高线图
使用基于状态的接口绘制等高线图
Matplotlib子模块允许我们用不同的接口来绘制等高线。在这一节中,我们将研究matplotlib的模式,以类似于MATLAB界面的方式绘制轮廓。
让我们通过代码来了解如何使用这个子模块来绘制轮廓线。
导入库
在这个特殊的例子中,我们将主要使用两个库,与之前的例子类似--Matplotlib和Numpy。
import numpy as np
import matplotlib.pyplot as plt
变量的初始化
delta = 0.18
element_ofx = np.arange(1.8, 2.8, delta)
element_ofy = np.arange(1.5, 3.6, delta)
grid_ofX, grid_ofY = np.meshgrid(element_ofx, element_ofy)
grid_ofZ = (np.exp(grid_ofX + grid_ofY))
让我们看一下完整的代码,以便更好地理解。
# Importing libraries
import numpy as np
import matplotlib.pyplot as plt
# variable initialisation
delta = 0.18
element_ofx = np.arange(1.8, 2.8, delta)
element_ofy = np.arange(1.5, 3.6, delta)
grid_ofX, grid_ofY = np.meshgrid(element_ofx, element_ofy)
grid_ofZ = (np.exp(grid_ofX + grid_ofY))
# Contour plotting
plot = plt.contour(grid_ofX, grid_ofY, grid_ofZ)
grid_format = {}
numscale = ['1', '2', '3', '4', '5', '6', '7']
for lvls, s in zip(plot.levels, numscale):
grid_format[lvls] = s
plt.clabel(plot, plot.levels, inline = True,
fmt = grid_format, fontsize = 10)
plt.title('Contour in Matlab interface')
plt.show()
MATLAB界面中的等高线图
总结
这篇文章为你学习Matplotlib打下了良好的基础。所有的主题和概念都是以一种易于理解的方法提出的,因此读者可以很容易地抓住所有的基础知识。对整篇文章的良好概述将帮助你轻松地进一步冒险学习更高级的Matplotlib概念。