How to plot a simple vector field in Matplotlib ?
The quantity incorporating both magnitude and direction is known as Vectors. In simple words, we can say, Vector Field is an engagement or collaboration of such vectors in a subset of space. Vector fields are the key aspects of understanding our real-life surrounding.
For more intuition, you can think of a vector field as representing a multivariable function whose input and output spaces each have the same dimension. The length of arrows drawn in a vector field is usually not to scale, but the ratio of the length of one vector to another should be accurate.
In this article, we are going to discuss how to plot a vector field in python. In order to perform this task we are going to use the quiver() method and the streamplot() method in matplotlib module.
To plot a vector field using the quiver() method:
matplotlib.pyplot.quiver(X, Y, U, V, **kw)
Where X, Y define the Vector location and U, V are directional arrows with respect of the Vector location.
To plot a vector field using the streamplot() method:
matplotlib.pyplot.streamplot(X, Y, U, V, density=1, linewidth=None, color=None, **kw)
Where X, Y are evenly spaced grid[1D array] and U and V represent the stream velocity of each point present on the grid. Density is the no. of vector per area of the plot. Line width represents the thickness of streamlines.
Below are some examples which depict how to plot vector fields using matplotlib module:
Example 1: Plotting a single vector using quiver() method in matplotlib module.
Example 2: Generating multiple vectors using quiver() method.
Example 3: Plotting multiple vectors using streamplot() method in matplotlib module.