Pour tracer un champ vectoriel avec matplotlib, il existe la fonction quiver:
quiver(X, Y, U, V, C, **kw)
avec
- X coordonnée x de l'origine du vecteur
- Y coordonnée y de l'origine du vecteur
- U coordonnée x de l'extrémité du vecteur
- V coordonnée y de l'extrémité du vecteur
- C couleur du vecteur
Tracer un simple vecteur avec quiver
Exemple de comment tracer un simple vecteur:

import matplotlib.pyplot as pltimport numpy as npX = np.array((0))Y= np.array((0))U = np.array((2))V = np.array((-2))fig, ax = plt.subplots()q = ax.quiver(X, Y, U, V,units='xy' ,scale=1)plt.grid()ax.set_aspect('equal')plt.xlim(-5,5)plt.ylim(-5,5)plt.title('How to plot a vector in matplotlib ?',fontsize=10)plt.savefig('how_to_plot_a_vector_in_matplotlib_fig3.png', bbox_inches='tight')#plt.show()plt.close()
Tracer un simple champ vectoriel avec quiver

import matplotlib.pyplot as pltimport numpy as npX, Y = np.meshgrid(np.arange(-10, 10, 1), np.arange(-10, 10, 1))x_shape = X.shapeU = np.zeros(x_shape)V = np.zeros(x_shape)for i in range(x_shape[0]):for j in range(x_shape[1]):U[i,j] = 1.0V[i,j] = 1.0fig, ax = plt.subplots()q = ax.quiver(X, Y, U, V, units='xy' ,scale=2, color='red')ax.set_aspect('equal')plt.xlim(-5,5)plt.ylim(-5,5)plt.title('How to plot a vector field using matplotlib ?',fontsize=10)plt.savefig('how_to_plot_a_vector_field_in_matplotlib_fig1.png', bbox_inches='tight')#plt.show()plt.close()
Références
| Liens | Site |
|---|---|
| matplotlib.pyplot.quiver | matplotlib doc |
| pylab_examples example code: quiver_demo.py | matplotlib doc |
| Plotting a vector field: quiver | scipy-lectures.org |
| Vector field | wikipedia |
| How can I set the aspect ratio in matplotlib? | stackoverflow |
