rh1990
rh1990

Reputation: 940

Numpy gradient of 2D array

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:

gradients

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?

Upvotes: 2

Views: 14974

Answers (1)

anishtain4
anishtain4

Reputation: 2402

I'm not sure why you think the gradients should be different. But the gradient shapes for the given function should have the same shape with different levels. Let's do it analytically:

partial derivatives

So you can see they both have the same shape. Add the color levels and you'll see the difference.

Upvotes: 5

Related Questions