spline interpolation and draw image by matplotlib
1 # spline interpolation and draw image by matplotlib 2 3 from scipy import interpolate 4 import matplotlib.pyplot as plt 5 6 #prepare data of globe 7 x_g = [0, 1, 2, 3, 4, 5] 8 y_g = [12,14,22,39,58,77] 9 10 x_points=x_g.copy() 11 y_points=y_g.copy() 12 13 # spline interpolation 14 def f(x): 15 tck = interpolate.splrep(x_points, y_points) 16 return interpolate.splev(x, tck) 17 18 # a test point 1.25/ ret=f(1.25) 19 p1=f(1.25) 20 print(p1.tolist()) # ndarray to list 21 x_g.append(1.25) 22 y_g.append(p1.tolist()) 23 24 # another test point [2.6,3.1,4.5]/ ret=f(m) 25 m=[2.6,3.1,4.5] 26 n=f(m) 27 print(f(m)) 28 x_g.extend(m) 29 y_g.extend(n) 30 31 x_g.sort() 32 y_g.sort() 33 plt.plot(x_g,y_g) 34 plt.gray() 35 plt.grid(True) 36 37 # add label for each point 38 for i in range(1,len(x_g)): 39 plt.text(x_g[i],y_g[i],str([x_g[i],y_g[i]]), family='serif', style='italic', ha='right', wrap=True) 40 41 plt.show() 42