メキシカンハット

3次元で Mexican hat を描いてみます:

import numpy as np
import matplotlib.pyplot as plt

def hat(x, y):
    r = x**2 + y**2
    return(np.exp(-r/2) * (1 - r/2) / np.pi)

t = np.linspace(-4, 4)
x, y = np.meshgrid(t, t)
z = hat(x, y)

fig, ax = plt.subplots(figsize=(8,5), subplot_kw=dict(projection="3d"))
ax.plot_surface(x, y, z)
Mexican hat

対話型環境なら、マウスでぐりぐりできます。

等高線を描くには plt.contour を使います:

plt.contour(x, y, z) # 塗りつぶすなら plt.contourf(x, y, z)

いろいろ指定できます:

cs = plt.contour(x, y, z, levels=[-0.06,-0.04,-0.02,0,0.1,0.2,0.3])
plt.axis("scaled")  # または plt.axis("equal")
cs.clabel()  # あるいは例えば cs.clabel([-0.04,0,0.2], fmt="%g")
Mexican hat

fmt=... オプションは、例えば "%.2f" なら小数第2位までに揃えて表示、"%g" なら精度を保ちできるだけ短い表示にします。