[Scipy-tickets] [SciPy] #1756: interp2d uses a confusing index order, webpage documentation wrong
SciPy Trac
scipy-tickets@scipy....
Mon Oct 29 04:05:52 CDT 2012
#1756: interp2d uses a confusing index order, webpage documentation wrong
---------------------------------+------------------------------------------
Reporter: aviolov | Owner: somebody
Type: defect | Status: new
Priority: low | Milestone: Unscheduled
Component: scipy.interpolate | Version: 0.9.0
Keywords: interp2d, meshgrid, |
---------------------------------+------------------------------------------
The code provided on the interp2d Examples page:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp2d.html
only works because the x, y arrays and the x_new, y_new arrays are exactly
the same size/shape. If they were different it will break - consider the
following variation, where the x,y,x_new, y_new arrays are of different
size:
{{{
#######################################
from scipy import interpolate
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(-5.01, 5.01, 0.25)
y = np.arange(-5.01, 5.01, 0.5) # y != x
xx, yy = np.meshgrid(x, y)
z = np.sin(xx**2+yy**2)
f = interpolate.interp2d(x, y, z, kind='cubic')
xnew = np.arange(-5.01, 5.01, 1e-2)
ynew = np.arange(-5.01, 5.01, 1e-1) # y_new != x_new
znew = f(xnew, ynew)
plt.figure()
plt.plot(x, z[:, 0], 'ro-', xnew, znew[:, 0], 'b-') #THis is what is on
the scipy doc pages and it won't work = x, z[:, 0] won't have same
dimesion
plt.figure()
plt.plot(x, z[0,:], 'ro-', xnew, znew[:, 0], 'b-') #THis is will also not
work - now x_new, znew[:, 0] won't have same dimension
plt.figure()
plt.plot(x, z[0,:], 'ro-', xnew, znew[0,:], 'b-') #This works
plt.show()
#######################################
}}}
The basic problem is that the indexes in z that corresponds to x is the
second dimension z[ydx, :], rather than the first y[:,ydx] as one would
expect intuitively - and as the writers of the documentation apparently
thought too.
i.e. meshgrid and interp2d, transpose the dimensions, or rather they use
Fortran ordering. I guess this way of doing things makes sense if you are
a Fortran person, but for me it is confusing that to index the values
along x of a tabulation of a function you need to access the second index
of an array, and, in any case, the documentation seems (is) wrong.
--
Ticket URL: <http://projects.scipy.org/scipy/ticket/1756>
SciPy <http://www.scipy.org>
SciPy is open-source software for mathematics, science, and engineering.
More information about the Scipy-tickets
mailing list