我需要在Python中创建一个可视化效果,就像colah在他的网站上做的那样。然而,我无法在matplotlib中找到任何与他这里所做的完全相同的网格变形方法。请问您能帮帮我吗?
回答:
我猜测这张图片是通过在网格上添加一些高斯函数生成的。
import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.collections import LineCollectiondef 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()