<p dir="auto">[See PR <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="240176526" data-permission-text="Title is private" data-url="https://github.com/numpy/numpy/issues/9355" data-hovercard-type="pull_request" data-hovercard-url="/numpy/numpy/pull/9355/hovercard" href="https://github.com/numpy/numpy/pull/9355">#9355</a>]</p>
<p dir="auto">I'm trying to calculate the gradient (df/dx, df/dy) of a 2D function f(x, y) with unevenly spaced data.</p>
<p dir="auto">Is there a function do to this in numpy? What is the correct syntax?</p>
<p dir="auto">I've tried using  <a href="https://numpy.org/doc/stable/reference/generated/numpy.gradient.html" rel="nofollow"><code class="notranslate">numpy.gradient</code></a> but can't get the syntax right.</p>
<p dir="auto">For example:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np

x = np.array([0, 1, 2, 2, 0])
y = np.array([0, 1, 0, 2, 2])
f = x + y"><pre class="notranslate"><code class="notranslate">import numpy as np

x = np.array([0, 1, 2, 2, 0])
y = np.array([0, 1, 0, 2, 2])
f = x + y
</code></pre></div>
<p dir="auto">I expect <code class="notranslate">np.gradient(f, x, y))</code> to calculate the right values ([[1, 1], [1, 1], [1, 1], [1, 1], [1, 1]]) but I'm getting an error.</p>
<p dir="auto"><code class="notranslate">f</code> is <em>An N-dimensional array containing samples of a scalar function.</em>, and after that <code class="notranslate">x, y</code> is <em>N arrays to specify the coordinates of the values along each dimension of F. The length of the array must match the size of the corresponding dimension</em> (definition 3. of *varargs).</p>
<p dir="auto">Other variants (e.g. <code class="notranslate">np.gradient(f, np.array([x, y]))</code> don't work either. I can't find a similar example in the documentation. Is there perhaps a different function for this type of gradient calculation? Many thanks</p>