2

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?

1
  • My guess is you're doing everything correctly. And while it's possible that the two gradients are the same, from the pictures I don't think they are (try 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 like sin(a*x + b*y) - which has periodic gradients) Commented Jun 15, 2018 at 12:10

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.