如何使用matplotlib(python)colah的变形网格进行绘图? [英] How to plot using matplotlib (python) colah's deformed grid?

查看:315
本文介绍了如何使用matplotlib(python)colah的变形网格进行绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Python中创建可视化效果,就像colah在他的网站上所做的一样.但是,我在matplotlib上找不到网格的任何失真以完全像他那样执行

I need to create a visualization in Python just like colah's did on his site. However, I could not find any distortion to grid on matplotlib to perform exactly like he did here. Pls, help me if you can.

这是我需要执行的情节:

This is the plot I need to perform:

推荐答案

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

I would guess the image is produced by adding some gaussian function to the grid.

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()

这篇关于如何使用matplotlib(python)colah的变形网格进行绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆