I have a 2D array sinusoidal pattern and want to plot both the x and y gradient of that function. I have a 2D array image_data:
def get_image(params):
# do some maths on params
return(image)
params = (a, b, c)
image_data = get_image(params)
I use numpy.gradient to get the gradients:
gradients = numpy.gradient(image_data)
x_grad = gradients[0]
y_grad = gradients[1]
Plotting all three looks like:
This pattern is not at 45 degrees. I'd expect x and y gradients to be different. In my mind x_gradient[i][j] should be the gradient of image_data[i][j] with respect to the indexes either side and y_gradient[i][j] the gradient with respect to indexes above and below. Because the pattern is angled slightly, in image_data the gradient changes at different rates.
Am I misinterpreting my data or not understanding numpy.gradient output?


print(np.all(x_gradient == y_gradient))). But even if they were, it's certainly possible for the two gradients to be the same depending on your generating function (it looks likesin(a*x + b*y)- which has periodic gradients)