{"id":355,"date":"2019-01-28T23:55:21","date_gmt":"2019-01-29T07:55:21","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=355"},"modified":"2019-01-28T23:55:21","modified_gmt":"2019-01-29T07:55:21","slug":"bivariate-spline","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/bivariate-spline\/","title":{"rendered":"Bivariate Spline"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport scipy.interpolate\n#from mpl_toolkits.mplot3d import Axes3D\nos.chdir(r'D:\\projects\\wordpress\\ex58')\nos.getcwd()\nx=y=np.arange(10)\nf=(lambda i,j: np.sin(i)*np.cos(j)) # function to interpolate\nA=np.fromfunction(f, (10,10)) # generate samples\nspline=scipy.interpolate.RectBivariateSpline(x,y,A)\nfig=plt.figure()\nsubplot=fig.add_subplot(111,projection='3d')\nxx=np.mgrid&#x5B;0:9:100j, 0:9:100j] # larger grid for plotting\nA=spline(np.linspace(0,9,100), np.linspace(0,9,100))\nsubplot.plot_surface(xx&#x5B;0],xx&#x5B;1],A)\nplt.xlabel('x')\nplt.ylabel('y')\nplt.savefig(&quot;example58.png&quot;, dpi=100)\nplt.show()\nplt.close()\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example58.png?resize=600%2C400&#038;ssl=1\" alt=\"example58\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-356\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example58.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example58.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np import scipy.interpolate #from mpl_toolkits.mplot3d import Axes3D os.chdir(r&#8217;D:\\projects\\wordpress\\ex58&#8242;) os.getcwd() x=y=np.arange(10) f=(lambda i,j: np.sin(i)*np.cos(j)) # function to interpolate A=np.fromfunction(f, (10,10)) # generate samples spline=scipy.interpolate.RectBivariateSpline(x,y,A) fig=plt.figure() subplot=fig.add_subplot(111,projection=&#8217;3d&#8217;) xx=np.mgrid&#x5B;0:9:100j, 0:9:100j] # larger grid for plotting A=spline(np.linspace(0,9,100), np.linspace(0,9,100)) subplot.plot_surface(xx&#x5B;0],xx&#x5B;1],A) plt.xlabel(&#8216;x&#8217;) plt.ylabel(&#8216;y&#8217;) plt.savefig(&quot;example58.png&quot;, dpi=100) plt.show() plt.close()<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-355","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":null,"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-5J","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":130,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/spline-interpolation-2\/","url_meta":{"origin":355,"position":0},"title":"Spline interpolation","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from scipy import interpolate os.chdir(r'D:\\data\\scripts\\web1\\ex26') os.getcwd() def runge(x): return 1\/(1 + 25 * x**2) x = np.linspace(-1, 1, 11) y = runge(x) f = interpolate.interp1d(x, y, kind=3) xx = np.linspace(-1, 1, 100) fig, ax = plt.subplots(figsize=(8, 4)) ax.plot(xx, runge(xx), 'k',\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example26","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example26.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example26.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example26.png?resize=525%2C300 1.5x"},"classes":[]},{"id":133,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/spline-interpolation-3\/","url_meta":{"origin":355,"position":1},"title":"#27 Spline interpolation","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from scipy import interpolate os.chdir(r'D:\\data\\scripts\\web1\\ex27') os.getcwd() x = np.array([0, 1, 2, 3, 4, 5, 6, 7]) y = np.array([3, 4, 3.5, 2, 1, 1.5, 1.25, 0.9]) xx = np.linspace(x.min(), x.max(), 100) fig, ax = plt.subplots(figsize=(8, 4)) ax.scatter(x, y) for n\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example27","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example27.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example27.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example27.png?resize=525%2C300 1.5x"},"classes":[]},{"id":352,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/spline\/","url_meta":{"origin":355,"position":2},"title":"Spline","author":"gantovnik","date":"2019-01-28","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np import scipy.interpolate os.chdir(r'D:\\projects\\wordpress\\ex57') os.getcwd() x=np.linspace(0,15,10) xn=np.linspace(0,15,50) y=np.sin(x) + 0.2*x interp=scipy.interpolate.InterpolatedUnivariateSpline(x,y) plt.plot(x,y,'o',xn,interp(xn)) plt.xlabel('x') plt.ylabel('y') plt.legend([\"$y(x)$\", \"$s(x)$\"],loc='best') plt.tight_layout() plt.savefig(\"example57.png\", dpi=100) plt.show() plt.close() [\/code] \u00a0","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example57.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example57.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example57.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1123,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/199-cubic-spline-interpolation-using-scipy\/","url_meta":{"origin":355,"position":3},"title":"#199 Cubic spline interpolation using scipy","author":"gantovnik","date":"2021-11-20","format":false,"excerpt":"[code language=\"python\"] # Cubic spline interpolation from scipy.interpolate import CubicSpline import numpy as np import matplotlib.pyplot as plt plt.style.use(\"seaborn-poster\") x = [0, 1, 2] y = [1, 3, 2] # use bc_type = \"natural\" adds the constraints f = CubicSpline(x, y, bc_type=\"natural\") x_new = np.linspace(0, 2, 100) y_new = f(x_new)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex199-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex199-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex199-1.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex199-1.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":73,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/spline-interpolation\/","url_meta":{"origin":355,"position":4},"title":"Spline interpolation","author":"gantovnik","date":"2018-12-24","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from scipy.interpolate import spline os.chdir('\/home\/vg\/Downloads\/projects\/ex11') os.getcwd() plt.figure(figsize=(10,8)) xp = np.linspace(0,1,6) yp = np.sqrt(1-xp**2) xi = np.linspace(0,1,100) yi = np.interp(xi,xp,yp) ys = spline(xp,yp,xi) plt.plot(xp,yp,'o',label='given points',lw=2) plt.plot(xi,yi,'--',label='piecewise linear',lw=2) plt.plot(xi,ys,'-',label='spline',lw=2) plt.legend(loc='best') plt.grid() plt.xlabel('x') plt.ylabel('y') plt.title(r'Spline interpolation of $y=\\sqrt{1-x^2}$') plt.axis('scaled') plt.axis([0,1.2,0,1.2]) plt.savefig(\"example11.png\", dpi=100) plt.show()\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example11","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example11.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example11.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example11.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1363,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/01\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3\/","url_meta":{"origin":355,"position":5},"title":"#259 Interpolation in python","author":"gantovnik","date":"2022-01-11","format":false,"excerpt":"[code language=\"python\"] import numpy as np from numpy import array , poly1d , polyfit from scipy.interpolate import interp1d import matplotlib.pyplot as plt # Data points xdata = array ([2,6,12,18,22,33,40,45,50,57]) ydata = array ([4.5 ,37 ,120 , 100 ,83.9 ,65 ,64 ,66 ,70 ,71]) fig,ax = plt.subplots() new_x=np.linspace(xdata[0],xdata[-1],num=100) #linear lin =\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/01\/ex259.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/01\/ex259.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/01\/ex259.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=355"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/355\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}