如何使用 matplotlib (python) colah 的变形网格进行绘图?

Fab*_*gel 7 python grid machine-learning matplotlib distortion

我需要在 Python 中创建一个可视化,就像 colah 在他的网站上所做的那样。但是,我无法在 matplotlib 上找到网格的任何失真以完全像他在这里所做的那样执行。请帮助我,如果可以的话。

这是我需要执行的情节:
colah 的向量场空间失真

Imp*_*est 10

我猜图像是通过向网格添加一些高斯函数产生的。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

def plot_grid(x,y, ax=None, **kwargs):
    ax = ax or plt.gca()
    segs1 = np.stack((x,y), axis=2)
    segs2 = segs1.transpose(1,0,2)
    ax.add_collection(LineCollection(segs1, **kwargs))
    ax.add_collection(LineCollection(segs2, **kwargs))
    ax.autoscale()


f = lambda x,y : ( x+0.8*np.exp(-x**2-y**2),y )

fig, ax = plt.subplots()

grid_x,grid_y = np.meshgrid(np.linspace(-3,3,20),np.linspace(-3,3,20))
plot_grid(grid_x,grid_y, ax=ax,  color="lightgrey")

distx, disty = f(grid_x,grid_y)
plot_grid(distx, disty, ax=ax, color="C0")

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 太感谢了。这正是我一直在寻找的! (2认同)