python - Interpolating values with scipy -


i'm working on short code snippet understand how scipy interpolates values on curve. in particular, i'm interested in finding values along interpolated curve. here's code i'm using:

import numpy np import matplotlib.pyplot plt scipy.interpolate import interp1d  x2 = [0, 1, 10, 25, 30, 50, 51, 89] y2 = [5, 12, 50, 73, 23, 34, 41, 50]  #x2 = [10, 16, 22, 29, 38, 47, 59, 66, 75, 86, 100, 113, 119, 136, 142, #              148, 154, 161, 171, 184, 192, 202, 223, 236, 246, 251, 262, 269, #              283, 297, 306, 316, 322, 331, 346, 358, 367, 373, 388, 397, 410, #              418, 423, 429, 439, 446, 467, 483, 497, 507, 520, 531, 538, 545, #              554, 575, 582, 588, 600, 608, 613, 627, 635, 655, 662, 676, 682, #              687, 695, 705, 712, 717, 724, 732, 740, 747, 755, 761, 771, 778, #              791, 802, 809, 816, 824, 834, 856, 863, 872] # #y2 = [23085.967, 14267.121, 20750.584, 9210.3926, 18401.682, 16611.393, #         24010.594, 17857.979, 21824.512, 11575.269, 11933.196, 20158.152, #         21785.586, 18465.51, 21024.26, 18313.832, 17494.381, 12609.491, #         16891.111, 27774.855, 31469.145, 13223.133, 18203.275, 28068.084, #         20896.027, 26155.961, 37014.309, 27647.0, 30435.248, 26335.934, #         24864.732, 47806.613, 32066.066, 29205.113, 24725.76, 32330.768, #         40981.988, 40529.309, 66155.766, 37105.953, 60022.656, 66403.039, #         52921.746, 64759.234, 54155.449, 56941.07, 55892.793, 36600.805, #         28585.988, 46508.383, 35625.566, 34409.988, 44258.063, 33728.02, #         32427.535, 18545.475, 20671.859, 26086.643, 31114.369, 36360.469, #         34226.172, 23922.705, 14892.418, 19938.51, 14699.634, 17899.898, #         13005.599, 17209.152, 16087.814, 10927.32, 12055.782, 17728.551, #         12807.264, 6844.522, 10761.619, 10167.683, 9643.835, 8896.9131, #         7541.904, 20032.299, 10911.229, 14315.814, 9069.4775, 10645.499, #         4797.4131, 12677.252, 3265.7893, 3370.4456, 5179.7227]  xinterp = np.arange(max(x2)) f = interp1d(x2, y2) f2 = interp1d(x2, y2, kind='cubic') y_lin = f(xinterp) y_cub = f2(xinterp) plt.scatter(x2, y2, marker = 'o', color = 'blue') plt.scatter(xinterp, f(xinterp),  marker = '.', color = 'green') plt.scatter(xinterp, f2(xinterp),  marker = '.',  color = 'cyan') plt.legend(['data', 'linear', 'cubic'], loc='best') plt.show() 

the code works fine short arrays, when uncomment longer versions of x2 , y2 (which measurements want interpolate between), get

  file "<ipython-input-272-5bc4611ab06a>", line 1, in <module>     runfile('c:/users/jeremy/dropbox/astro480/image_analysis/minimal_interp2.py', wdir='c:/users/jeremy/dropbox/astro480/image_analysis')    file "c:\users\jeremy\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile     execfile(filename, namespace)    file "c:\users\jeremy\anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile     exec(compile(f.read(), filename, 'exec'), namespace)    file "c:/users/jeremy/dropbox/astro480/image_analysis/minimal_interp2.py", line 36, in <module>     y_lin = f(xinterp)    file "c:\users\jeremy\anaconda3\lib\site-packages\scipy\interpolate\polyint.py", line 79, in __call__     y = self._evaluate(x)    file "c:\users\jeremy\anaconda3\lib\site-packages\scipy\interpolate\interpolate.py", line 610, in _evaluate     below_bounds, above_bounds = self._check_bounds(x_new)    file "c:\users\jeremy\anaconda3\lib\site-packages\scipy\interpolate\interpolate.py", line 639, in _check_bounds     raise valueerror("a value in x_new below interpolation "  valueerror: value in x_new below interpolation range. 

x2 , y2 same length in each case (14 in short version, 89 in longer).

why interpolation work on 1 set of lists, not other?


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -