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()
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?