Python插值并为x和y提取z的值?

时间:2017-11-09 11:49:50

标签: python-3.x interpolation curve-fitting curve linear-interpolation

你能帮忙吗? 我有这个数据,其中z是特定x和y的函数

xs = [0.15, 0.35, 0.5, 0.67, 0.8]
ys = [0.01,0.01, 0.01, 0.01, 0.01]
z = [0.75, 0.83, 1.00, 0.92, 0.91]

我将这些值排列在这个形状中 如何对点进行插值,以便稍后可以将z值调用为与我的值不同?

2 个答案:

答案 0 :(得分:2)

public function orders()
{
    return $this->hasManyThrough(
                'App\Orders',
                'App\Coupons',
                'user_id', // Foreign key on Coupons table...
                'coupon_code', // Foreign key on Orders table...
                'id', // Local key on Users table...
                'coupon_code' // Local key on Coupons table...
            );
}

现在通过运行代码,它将为特定的x和y提供z。 这可以在没有图的情况下使用。只需将其置于值之下,并确保以相同的方式排列值

答案 1 :(得分:1)

简单的搜索已经有所帮助。 你的问题基本上就是这个例子 scipy.interpolate.interp2d documentation

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

xs = [ 0.15, 0.35, 0.5, 0.67, 0.8 ]
ys = [ 0.01, 0.05, 0.1, 0.2, 0.3 ]
zz = np.array( [
    0.75, 0.83, 1.00, 0.92, 0.91,
    0.75, 0.82, 0.87, 0.88, 0.88,
    0.74, 0.81, 0.84, 0.83, 0.83,
    0.72, 0.76, 0.77, 0.76, 0.76,
    0.72, 0.72, 0.72, 0.72, 0.72 
    ] ).reshape( ( 5, 5 ) )

xx, yy = np.meshgrid( xs, ys )
f = interpolate.interp2d( xx, yy, zz, kind='cubic' )

fig = plt.figure()
ax = fig.add_subplot( 1, 1, 1, projection='3d' )
ax.plot_surface( xx, yy, zz)

x2 = np.linspace( .15,.8,50 )
y2 = np.linspace( .01,.3,50 )

xx2, yy2 = np.meshgrid( x2, y2 )
zz2 = f( x2, y2 )

fig2 = plt.figure()
bx = fig2.add_subplot( 1, 1, 1, projection='3d' )
bx.plot_surface( xx2, yy2, zz2 )

plt.show()

提供原始数据

original data

和50乘50格的三次插值

cubic interpolation