Plot warped a 2D mesh using Python

I want to build a deformed rectangular grid, which means that the coordinates of the nodes depend on the indices of the node. The goal is to visualize the deformation of a unit square by a function.

How can i do this in python?

+4
source share
1 answer

This is the type of thing for which it is intended pcolormesh(or pcolor). (Also look at triplotetc. For triangular meshes.)

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

In a side note, pcolormeshthe default is not used without smoothing for performance reasons. If you add antiailiased=Trueto the call pcolormesh, you get a nice result:

enter image description here

+6
source

Source: https://habr.com/ru/post/1606450/


All Articles