Python – scipy.interpolate.interp2d problem – s or m too small – no more knots can be added

scipy.interpolate.interp2d problem – s or m too small – no more knots can be added… here is a solution to the problem.

scipy.interpolate.interp2d problem – s or m too small – no more knots can be added

I read these three posts that are somewhat similar :

Issues with 2D-Interpolation in Scipy

Problem with 2D interpolation in SciPy, non-rectangular grid

https://groups.google.com/forum/#!topic/scipy-user/eJZKWvvR86Y

But their situation is different from mine because my data is not on a grid at all. Do you have any suggestions for dealing with this?

Thanks

Python 2.7.13 (default, Jan  4 2017, 16:52:04)[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from math import *
>>> from scipy import interpolate
>>> z = [-0.3096805512905121, -0.355350136756897, -0.18027666211128235, -0.24593926966190338, -0.3015348017215729, -0.3483999967575073, 0.04897193983197212, - 0.08004648983478546, -0.16770946979522705, -0.23645322024822235, -0.2939966320991516, -0.34211716055870056, 0.30726158618927, 0.07429734617471695, -0.06275345385074615, - 0.15545858442783356, -0.22740797698497772, -0.2870173752307892, -0.3364697992801666, 0.3395797610282898, 0.09856464713811874, -0.045787736773490906, -0.14348694682121277, - 0.2187458723783493, -0.2805372476577759, -0.331415057182312, 0.7114291191101074, 0.36744925379753113, 0.12159627676010132, -0.029206041246652603, -0.13178685307502747, - 0.2104278802871704, -0.27450671792030334, -0.32690396904945374, 0.7232922911643982, 0.3910365402698517, 0.14326415956020355, -0.013096758164465427, -0.12038511037826538, - 0.20243625342845917, -0.26888760924339294, -0.3228927254676819, 0.9531660676002502, 0.7284945845603943, 0.4108444154262543, 0.16354575753211975, 0.0024749785661697388, - 0.10931339114904404, -0.19476951658725739, -0.26365524530410767, -0.3193519413471222, 0.939074695110321, 0.7292525768280029, 0.42768344283103943, 0.1826346516609192, 0.01754613406956196, -0.09856594353914261, -0.18741914629936218, -0.2587881088256836, -0.31625133752822876, -0.25721806287765503, -0.3091379702091217, -0.3552372455596924, - 0.39388224482536316, -0.09650994092226028, -0.1798516809940338, -0.24300941824913025, -0.2973443865776062, -0.34505653381347656, -0.3849600851535797, 0.3623177409172058, 0.08555295318365097, -0.07005918771028519, -0.16187138855457306, -0.22936709225177765, -0.28620466589927673, -0.3355335295200348, -0.3766307532787323, 0.771324634552002, 0.4340214431285858, 0.129726380109787, -0.04378318414092064, -0.14426229894161224, -0.21626389026641846, -0.2757115960121155, -0.32669389247894287, -0.36895614862442017, 1.014732837677002, 0.8375164270401001, 0.49998506903648376, 0.17260998487472534, -0.017848851159214973, -0.1269984096288681, -0.20365294814109802, -0.265836238861084, - 0.31853875517845154, -0.3619702160358429, 1.0408343076705933, 0.8884564638137817, 0.5574558973312378]
>>> y = [-1.3547110781316547, -1.3584429517142182, -1.3609724102437497, -1.3701066100829191, -1.3769244964793548, -1.3811451969518873, -1.354711107502558, - 1.3687735759920039, -1.3811452478830406, -1.391417878645462, -1.3991661804055002, -1.404002157019965, -1.354711107502558, -1.371449601166359, -1.386938582998325, - 1.4007617296244617, -1.4124153735237703, -1.421335111051171, -1.4269690947160343, -1.3687735759920039, -1.386938582998325, -1.404002215570618, -1.419506402792546, - 1.4328463811203447, -1.4432743761364633, -1.4499827693866287, -1.3609724102437497, -1.3811452478830406, -1.4007617296244617, -1.419506402792546, -1.4369154633875083, - 1.452306817753661, -1.4647170919070276, -1.4729411157817642, -1.3701066100829191, -1.391417878645462, -1.4124153735237703, -1.4328463811203447, -1.452306817753661, - 1.4701230202565743, -1.4851603365514905, -1.495646234129088, -1.3547110781316547, -1.3769244964793548, -1.3991661804055002, -1.421335111051171, -1.4432743761364633, - 1.4647170919070276, -1.4851603365514905, -1.5035549846166696, -1.517607425698032, -1.3584429517142182, -1.3811451969518873, -1.404002157019965, -1.4269690947160343, - 1.4499827693866287, -1.4729411157817642, -1.495646234129088, -1.517607425698032, -1.537137910311919, -1.2715464788626845, -1.277789529443993, -1.2823516395271821, - 1.2851319947805375, -1.274202133611472, -1.2842015887238418, -1.2927131884504137, -1.2995585210000689, -1.3045781726257122, -1.3076449947768283, -1.2680507888912849, - 1.2814321895863856, -1.2936786382992593, -1.304578208934795, -1.3139097013479977, -1.3214542026311582, -1.327011144220908, -1.3304171683857975, -1.269792426312085, - 1.2851322059512371, -1.2995586985275607, -1.312853221114593, -1.3247709666006957, -1.3350475569351674, -1.3434122728957845, -1.3496090075349574, -1.3534233908342608, - 1.2680507888912849, -1.2851322059512371, -1.3015531091904444, -1.317110941842435, -1.3315652281664823, -1.344636210235179, -1.3560089389387469, -1.3653467678101243, - 1.3723177198055692, -1.3766336232467964, -1.2814321895863856, -1.2995586985275607, -1.317110941842435]
>>> x = [-1.7894636598330216, -1.6814517307495056, -2.0344433397476207, -1.9295661805612403, -1.8157732368389208, -1.6951493039482421, -2.2794224586759544, - 2.191045490589519, -2.089941890843064, -1.975687290944475, -1.8490940739467878, -1.712691116415809, -2.4329665217087353, -2.356194490192345, -2.2655344075659287, - 2.158798471843708, -2.034443141064453, -1.8925447950213865, -1.7359424267026846, -2.521343489795171, -2.4468545728187614, -2.356194490192345, -2.2455369782630465, - 2.11121512583173, -1.9513004430318341, -1.7681889064008147, -2.677945640637069, -2.622447089541626, -2.5535905085409816, -2.4668520021216436, -2.356194490192345, - 2.2142969587489763, -2.0344415515976904, -1.8157714837493195, -2.7828227998234496, -2.736701689440215, -2.6779458393202367, -2.6011738545529597, -2.4980920216357134, - 2.356194490192345, -2.158796637841327, -1.8925427088400433, -2.922925320551668, -2.896615743545769, -2.863294906437902, -2.819844185363303, -2.761088537352856, - 2.6779474287869993, -2.5535923425433626, -2.356194490192345, -2.034440359475236, -3.0309372496351843, -3.0172396764364477, -2.999697863968881, -2.9764465536820053, - 2.9442000739838754, -2.89661749663537, -2.8198462715446464, -2.677948620909454, -2.356194490192345, -1.8925461361322136, -1.8157741718174394, -1.7359436349113315, - 1.6539361199482736, -2.0701424410380467, -1.99742312589383, -1.9195665472506898, -1.8370475047995038, -1.7506483483895765, -1.6614546505925627, -2.2455368328860743, - 2.181521771142585, -2.1112152134870783, -2.034443220538601, -1.9513018817726857, -1.862252191657338, -1.768190281901339, -1.67046326786262, -2.356194490192345, - 2.2974385852631256, -2.2318393122462323, -2.1587986246768605, -2.077894381339721, -1.989020041891684, -1.8925460864609207, -1.7894636598330216, -1.6814517307495056, - 2.466852147498616, -2.414950395121564, -2.356194490192345, -2.289626220921307, -2.2142971971689764, -2.1293952403083103, -2.0344433397476207, -1.9295661805612403, - 1.8157732368389208, -1.6951493039482421, -2.5308672092421047, -2.4805496681384573, -2.422762759463383]
>>> f = interpolate.interp2d(x,y,z)
>>>     /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy/interpolate/_fitpack_impl.py:975: RuntimeWarning: No more knots can be added because the number of B-spline coefficients already exceeds the number of data points m. Probable causes: either s or m too small. (fp>s) kx,ky=1,1 nx,ny=13,12 m=100 fp=0.000879 s=0.000000 warnings.warn(RuntimeWarning(_iermess2[ierm][0] + _mess))

Solution

For unstructured arrays, you need to use interpolate.griddata. This is a naïve quick solution :

import numpy as np
from scipy import interpolate
from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D

xx = np.linspace(np.min(x), np.max(x))
yy = np.linspace(np.min(y), np.max(y))
xx, yy = np.meshgrid(xx, yy)

vals = interpolate.griddata((x, y), z, (xx.ravel(), yy.ravel()))
fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(xx.ravel(), yy.ravel(), vals)
pyplot.show()

Related Problems and Solutions