# 利用contour(),contourf()描绘等值线
# contourf()带有填充效果
def fig2():
y, x = np.ogrid[-2:2:200j, -3:3:300j]
z = x * np.exp(-x ** 2 - y ** 2)
extent = [np.min(x), np.max(x), np.min(y), np.max(y)]
plt.figure(figsize=(10, 4))
plt.subplot(121)
# 使用extent指定x轴和y轴的数据范围
# 10 表示将整个函数的范围等值分为10个区间
cs = plt.contour(z, 10, extent=extent)
plt.clabel(cs)
plt.subplot(122)
plt.contourf(x.reshape(-1), y.reshape(-1), z, 20)
plt.show()

def fig3():
y, x = np.ogrid[-1.5:1.5:200j, -1.5:1.5:200j]
f = (x ** 2 + y ** 2) ** 4 - (x ** 2 - y ** 2) ** 2
plt.figure(figsize=(9, 4))
plt.subplot(121)
extend = [np.min(x), np.max(x), np.min(y), np.max(y)]
cs = plt.contour(f, extend=extend, levels=[0, 0.1], colors=['b', 'r'], linestyles=['solid', 'dashed'],
linewidths=[2, 2])
plt.subplot(122)
for c in cs.collections:
data = c.get_paths()[0].vertices
plt.plot(data[:, 0], data[:, 1], color=c.get_color()[0], linewidth=c.get_linewidth()[0])
plt.show()
