8.等高线图
线:
mp.contour(x, y, z, 线数, colors=颜色,
linewidths=线宽)
色带:
mp.contourf(x, y, z, 线数, cmap=颜色映射)
import numpy as np
import matplotlib.pyplot as mp
n = 1000
# 栅格化(网格化)函数
x, y = np.meshgrid(np.linspace(-3, 3, n),
np.linspace(-3, 3, n))
z = (1 - x / 2 + x ** 5 + y ** 3) * \
np.exp(-x ** 2 - y ** 2)
mp.figure('Contour', facecolor='lightgray')
mp.title('Contour', fontsize=20)
mp.xlabel('x', fontsize=14)
mp.ylabel('y', fontsize=14)
mp.tick_params(labelsize=10)
mp.grid(linestyle=':')
mp.contourf(x, y, z, 8, cmap='jet')
# 等高线
cntr = mp.contour(x, y, z, 8, colors='black',
linewidths=0.5)
# 给等高线加标签(将z值写到图上)
mp.clabel(cntr, inline_spacing=1,
fmt='%.1f', fontsize=10)
# inline_spacing 空格线
mp.show()