在对数日志图上插入一条直线(NumPy)

时间:2016-07-20 16:58:32

标签: python numpy interpolation loglog

我在Raspberry Pi上使用一个程序来测量盐水中探针的电压,以计算水的盐度。当在对数 - 对数图上绘制功率趋势线时,该关系不是线性的,而是变成相当直线。这意味着只需使用两个值校准探针,并在对数 - 对数图上绘制时简单地插入它们之间的直线。

Salinity graphs

不幸的是,预先存在的程序使用标准轴呈现线性关系,我不知道如何将其更改为在对数 - 对数图上插入直线。任何帮助将不胜感激,请注意,这是我已经完成的第一个编码,所以我的知识不是很好。我已经包含了涉及下面插值的代码:

import smbus
import time

# imports for plotting

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate

# do the first plot - all values zero

nprobe=4

x=np.array([10.0, 30.0, 10.0, 30.0])
y=np.array([10.0, 10.0, 20.0, 20.0])
z=np.array([0., 0., 0., 0.])

# changing probe 1 to my handmade probe 1
fresh=np.array([0.,0.,0.,0.])
sea  =np.array([100.0,100.0,100.0,100.0])
range=np.array([100.0,100.0,100.0,100.0])
range=1.0*(sea-fresh)


# grid for plots - 20 is a bit coarse - was 100 give explicit (0,1) limits as no bcs here
###########   xi, yi = np.linspace(x.min(), x.max(), 50), np.linspace(y.min(), y.max(), 50)
xi, yi = np.linspace(0, 1, 50), np.linspace(0, 1, 50)
xi, yi = np.meshgrid(xi, yi)

rbf= scipy.interpolate.Rbf(x,y, z, function='linear')
zi= rbf(xi, yi)

plt.ion()

tank=plt.imshow(zi, vmin=0, vmax=50, origin='lower', extent=[0, 44, 0, 30])

plt.scatter(x, y, c=z)
plt.colorbar()

plt.draw()

此外,稍后在该计划中:

# make r1 an array, results between 0-100 where 0 is 0% salinity and 100 is 2.5% salinity
        z=100.0*(r1-fresh)/range

        print time.strftime("%a, %d %b %Y, %H:%M:%S")
        print "measured reading at above time (r1)"
        print r1[0],r1[1],r1[2],r1[3]
        print "fresh values used for calibration"
        print fresh
        print "range between calibration values"
        print range
        print "percentage seawater (z)"
        print z

# interpolate
        rbf= scipy.interpolate.Rbf(x,y, z, function='linear')
        zi= rbf(xi, yi)
# alt interpolate
#########       zi=scipy.interpolate.griddata((x,y), z, (xi,yi), method='linear')

        print "zi"
        print zi

1 个答案:

答案 0 :(得分:1)

怎么样

import numpy as np
import scipy
import scipy.interpolate

import matplotlib.pyplot as plt

def log_interp1d(x, y, kind='linear'):
    """
    Returns interpolator function
    """
    log_x = np.log10(x)
    log_y = np.log10(y)
    lin_int = scipy.interpolate.interp1d(log_x, log_y, kind=kind)
    log_int = lambda z: np.power(10.0, lin_int(np.log10(z)))
    return log_int

powerlaw = lambda x, amp, index: amp * (x**index)

num_points = 20

# original data
xx = np.linspace(1.1, 10.1, num_points)
yy = powerlaw(xx, 10.0, -2.0)

# get interpolator
interpolator = log_interp1d(xx, yy)

# interpolate at points
zz = np.linspace(1.2, 8.9, num_points-1)
# interpolated points
fz = interpolator(zz)

plt.plot(xx, yy, 'o', zz, fz, '+')
plt.show()