I have a 2D array that stores values of a property of each point as its element: f(x,y) = f[x][y]
. Now I want to find the gradient of this array. I looked into np.gradient
but it just gives two arrays as return, first with derivative in x direction and second in y direction.
I want to learn how can I use this or any other way to create a gradient map that shows the change in gradient of the 2D array.varray
is the 2D array I want to create gradient map of. Following is the only things I can think of right now. I know there should be clever way to use x gradient
and y gradient
generated by np.gradient()
but I cannot think of it.
lx
and ly
are x and y dimension of the 2D array.
vgrad = np.gradient(varray)
xgrad = vgrad[0]
x, y = range(0, lx), range(0,ly)
xi, yi = np.meshgrid(x, y)
rbf = scipy.interpolate.Rbf(xi, yi, xgrad)
plt.imshow(v, vmin = np.amin(xgrad), vmax=np.amax(xgrad))
plt.colorbar()
plt.show()
I want to get basically the second image from the first image. The second image is described as σ = \alpha*grad(varray)
.
Using magnitude of gradient as suggested by @Mad Physicist below.
vgrad = np.gradient(varray)
fulgrad = np.sqrt(vgrad[0]**2 + vgrad[1]**2)
plt.imshow(fulgrad,cmap=plt.get_cmap('hot'), vmin = np.amin(fulgrad),vmax = np.amax(fulgrad))
plt.colorbar()
plt.show()
the image i get :
I am interpreting this wrong from basic understanding of the equation?
So here is my images. On left: image of the initial 2D map. On right: Image of the gradient map. @Mad Physicist do you think they are similar to above with only difference of colors?
We can use the numpy. gradient() function to find the gradient of an N-dimensional array. For gradient approximation, the function uses either first or second-order accurate one-sided differences at the boundaries and second-order accurate central differences in the interior (or non-boundary) points.
gradient. Return the gradient of an N-dimensional array. The gradient is computed using second order accurate central differences in the interior points and either first or second order accurate one-sides (forward or backwards) differences at the boundaries.
We will use numdifftools to find Gradient of a function. Examples: Input : x^4+x+1 Output :Gradient of x^4+x+1 at x=1 is 4.99 Input :(1-x)^2+(y-x^2)^2 Output :Gradient of (1-x^2)+(y-x^2)^2 at (1, 2) is [-4.
If you are looking for the magnitude of the gradient, you can just do
mag = np.sqrt(vgrad[0]**2 + vgrad[1]**2)
Then plot mag
instead of xgrad
as above. If, you want to plot the gradient as a vector map or stream plot, do something like
plt.streamplot(xi, yi, vgrad[0], vgrad[1])
You may also be interested in the visual representation of the slope that can be obtained from just plotting the original surface in 3D:
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(xi, yi, varray)
plt.show()
See What is the equivalent of Matlab's surf(x,y,z,c) in matplotlib? and http://matplotlib.org/examples/mplot3d/surface3d_demo.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With