使用Python绘制变形的2D网格

时间:2015-09-08 16:10:59

标签: python matplotlib

我想绘制一个变形的矩形网格,这意味着节点的坐标取决于节点的索引。目的是通过函数可视化单位平方的变形。

我怎么能在python中做到这一点?

1 个答案:

答案 0 :(得分:6)

这是pcolormesh(或pcolor)的含义。 (另请查看三角形网格的triplot等。)

import matplotlib.pyplot as plt

y, x = np.mgrid[:10, :10]
z = np.random.random(x.shape)

xdef, ydef = x**2, y**2 + x

fig, axes = plt.subplots(ncols=2)
axes[0].pcolormesh(x, y, z, cmap='gist_earth')
axes[1].pcolormesh(xdef, ydef, z, cmap='gist_earth')

axes[0].set(title='Original', xticks=[], yticks=[])
axes[1].set(title='Deformed', xticks=[], yticks=[])

plt.show()

enter image description here

另外,pcolormesh默认使用无抗锯齿功能。如果您将antiailiased=True添加到pcolormesh来电,那么您将获得更好看的结果:

enter image description here

相关问题