Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot deformed 2D mesh with Python

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

How can I do that in python?

like image 210
shuhalo Avatar asked Mar 15 '23 20:03

shuhalo


1 Answers

This is the type of thing that pcolormesh (or pcolor) is meant for. (Also have a look at triplot, etc 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

On a side note, pcolormesh defaults to using no antialiasing for performance reasons. If you add antiailiased=True to the pcolormesh call, you'll get a nicer-looking result:

enter image description here

like image 67
Joe Kington Avatar answered Mar 24 '23 05:03

Joe Kington