Pour évaluer une fonction à deux variables en python, comme par example
\begin{equation}
f: (x_1,x_2) \rightarrow x_1 * \exp^{-(x_1^2+x_2^2)}
\end{equation}
le plus simple est d'utiliser la fonction numpy meshgrid.
Utiliser la fonction numpy meshgrid
Exemple
from pylab import figure, cmimport matplotlib.pyplot as pltimport numpy as npdef f(x1,x2):return x1 * np.exp(-(x1**2+x2**2))x1_min = -2.0x1_max = 2.0x2_min = -2.0x2_max = 2.0x1, x2 = np.meshgrid(np.arange(x1_min,x1_max, 0.1), np.arange(x2_min,x2_max, 0.1))y = f(x1,x2)
Tracer la fonction avec imshow
On peut alors utiliser imshow pour visualiser le résultat:
plt.imshow(y,extent=[x1_min,x1_max,x2_min,x2_max], cmap=cm.jet, origin='lower')plt.colorbar()plt.title("How to evaluate a 2D function using a python grid?" , fontsize=8)plt.savefig("evaluate_2d_function_using_meshgrid_03.png", bbox_inches='tight')plt.show()

On peut aussi tracer x1:
plt.imshow(x1, origin='lower', cmap=cm.jet)plt.title("How to evaluate a 2D function using a python grid?" , fontsize=8)plt.colorbar()plt.savefig("evaluate_2d_function_using_meshgrid_01.png", bbox_inches='tight')plt.show()plt.close()

ou x2
plt.imshow(x2, origin='lower', cmap=cm.jet)plt.title("How to evaluate a 2D function using a python grid?" , fontsize=8)plt.colorbar()plt.savefig("evaluate_2d_function_using_meshgrid_02.png", bbox_inches='tight')plt.show()plt.close()

Tracer avec la fonction matplotlib contour
On peut aussi utiliser contour
plt.contour(x1,x2,y,extent=[x1_min,x1_max,x2_min,x2_max], cmap=cm.jet, origin='lower')plt.colorbar()plt.title("How to evaluate a 2D function using a python grid?" , fontsize=8)plt.savefig("evaluate_2d_function_using_meshgrid_04.png", bbox_inches='tight')

Tracer en 3D
Pour visualiser en 3D, il existe plusieurs solutions, on peut par exemple utiliser la fonction contour3D:
from mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()ax = fig.add_subplot(111, projection='3d')ax.contour3D(x1,x2,y, 100,cmap=cm.jet)ax.view_init(60, 35)plt.title("How to evaluate a 2D function using a python grid?" , fontsize=8)plt.savefig("evaluate_2d_function_using_meshgrid_05.png", bbox_inches='tight')plt.close()

Code source
from pylab import figure, cmimport matplotlib.pyplot as pltimport numpy as npdef f(x1,x2):return x1 * np.exp(-(x1**2+x2**2))x1_min = -2.0x1_max = 2.0x2_min = -2.0x2_max = 2.0x1, x2 = np.meshgrid(np.arange(x1_min,x1_max, 0.1), np.arange(x2_min,x2_max, 0.1))plt.imshow(x1, origin='lower', cmap=cm.jet)plt.title("How to evaluate a 2D function using a python grid?" , fontsize=8)plt.colorbar()plt.savefig("evaluate_2d_function_using_meshgrid_01.png", bbox_inches='tight')#plt.show()plt.close()plt.imshow(x2, origin='lower', cmap=cm.jet)plt.title("How to evaluate a 2D function using a python grid?" , fontsize=8)plt.colorbar()plt.savefig("evaluate_2d_function_using_meshgrid_02.png", bbox_inches='tight')#plt.show()plt.close()y = f(x1,x2)plt.imshow(y,extent=[x1_min,x1_max,x2_min,x2_max], cmap=cm.jet, origin='lower')plt.colorbar()plt.title("How to evaluate a 2D function using a python grid?" , fontsize=8)plt.savefig("evaluate_2d_function_using_meshgrid_03.png", bbox_inches='tight')#plt.show()plt.close()plt.contour(x1,x2,y,extent=[x1_min,x1_max,x2_min,x2_max], cmap=cm.jet, origin='lower')plt.colorbar()plt.title("How to evaluate a 2D function using a python grid?" , fontsize=8)plt.savefig("evaluate_2d_function_using_meshgrid_04.png", bbox_inches='tight')plt.close()from mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()ax = fig.add_subplot(111, projection='3d')ax.contour3D(x1,x2,y, 100,cmap=cm.jet)ax.view_init(60, 35)plt.title("How to evaluate a 2D function using a python grid?" , fontsize=8)plt.savefig("evaluate_2d_function_using_meshgrid_05.png", bbox_inches='tight')plt.close()
Références
| Liens | Site |
|---|---|
| meshgrid | docs scipy |
| The glowing python | glowingpython |
| Density and Contour Plots | jakevdp.github.i |
| What is the purpose of meshgrid in Python / NumPy? | stackoverflow |
| How to merge mesh grid points from two rectangles in python? | stackoverflow |
