matplotlib.pyplot.contour

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""
demo04_contour.py 绘制等高线图
"""
import numpy as np
import matplotlib.pyplot as plt

# 整理数据
n = 500
x, y = np.meshgrid(np.linspace(-3, 3, n),
np.linspace(-3, 3, n))
# print(x, y)
# 计算每个坐标点的高度
z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y**2)
# 绘制等高线
plt.figure('Contour', facecolor='lightgray')
plt.title('Contour', fontsize=18)
plt.grid(linestyle=':')
cntr = plt.contour(x, y, z, 8, colors='black', linewidths=.5)
# 绘制等高线的高度标签文本
# inline_spacing 空白间距
plt.clabel(cntr, inline_spacing=1, fmt='%.2f',
fontsize=10)
# 填充等高线
plt.contourf(x, y, z, 8, cmap='jet')
plt.savefig('Contour.png')
plt.show()

等高线图Contour