有一个二维数组,比如[[1,2],[2,4]],我们想以此画出其海拔图,高度值就是数组上的值,请教具体可以怎么做?
等高线图也被称为等值线图,是一种在二维平面上显示三维表面的方法。Matplotlib API 提供了两个等值线图的绘制方法:contour( ) 函数用于绘制带轮廓线的等值线图,contourf( ) 函数用于绘制带填充色的等值线图。
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = ['FangSong'] # 设置字体以便正确显示汉字
plt.rcParams['axes.unicode_minus'] = False # 正确显示连字符
y, x = np.mgrid[-3:3:60j, -4:4:80j]
z = (1-y**5+x**5)*np.exp(-x**2-y**2)
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax1.set_title('无填充的等值线图')
c1 = ax1.contour(x, y, z, levels=8, cmap='jet') # 无填充的等值线
ax1.clabel(c1, inline=1, fontsize=12) # 为等值线标注值
ax2 = fig.add_subplot(122)
ax2.set_title('有填充的等值线图')
c2 = ax2.contourf(x, y, z, levels=8, cmap='jet') # 有填充的等值线
fig.colorbar(c1, ax=ax1) # 添加ColorBar
fig.colorbar(c2, ax=ax2) # 添加ColorBar
plt.show()