Quiver Plot using gradient
Let us create a quiver plot which shows the gradient function.The np, gradient() method of Numpy can be used to apply the gradient function to each arrow’s x, y starting position. The equation is used to create the following plot:
z = xe^{-x^2-y^2}
Example:
Python3
# Import libraries import numpy as np import matplotlib.pyplot as plt # Creating arrows x = np.arange( - 2 , 2.2 , 0.2 ) y = np.arange( - 2 , 2.2 , 0.2 ) # Creating gradient X, Y = np.meshgrid(x, y) z = X * np.exp( - X * * 2 - Y * * 2 ) dx, dy = np.gradient(z) # Creating plot fig, ax = plt.subplots(figsize = ( 9 , 9 )) ax.quiver(X, Y, dx, dy) ax.xaxis.set_ticks([]) ax.yaxis.set_ticks([]) ax.set_aspect( 'equal' ) # show plot plt.show() |
Output :
Quiver Plot in Matplotlib
Quiver plot is basically a type of 2D plot which shows vector lines as arrows. This type of plots are useful in Electrical engineers to visualize electrical potential and show stress gradients in Mechanical engineering.
Contact Us