如何插入不规则刻度的曲线?

时间:2017-11-12 01:25:26

标签: python curve-fitting spline smoothing

我想要在图表上显示以下粒度数据,其中x是粒度,y是概率密度函数。 x遵循几何序列(每个值乘以2)

x:[0.0,0.078130000000000005,0.15626000000000001,0.31252000000000002,0.62504000000000004,1.2500800000000001,2.5001600000000002,5.0003200000000003,10.0064000000000001,20.001280000000001]

y:[0.0,1.0512499897262986,0.8764973224043381,0.6221799472771921,0.3760124741123981,0.1934680804381​​7057,0.08474951460350254,0.0316071940839253,0.010035880788326037,0.0]

这是图表:

enter image description here

我一直试图像Excel一样平滑曲线。我尝试过使用interp1d(所有方法),spline,Akima1DInterpolator。

1 个答案:

答案 0 :(得分:1)

所以在我上面的评论中,我说很容易。然而,问题在于最终数据非常平坦。立方和高阶插值不喜欢平坦数据。他们倾向于振荡。正如@ f5r5e5d所提到的,诀窍是以平坦部分变得非常短的方式重新缩放数据。在这种情况下,双对数刻度似乎没问题。不幸的是,这不适用于y = 0值。因此,任何一个人都必须删除这个值或为它选择一个合理的小值(但不要太小,因为在双对数刻度上该点会偏离)。

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

xList = [0.078130000000000005, 0.15626000000000001, 0.31252000000000002, 0.62504000000000004, 1.2500800000000001, 2.5001600000000002, 5.0003200000000003, 10.000640000000001, 20.001280000000001]

yList = [ 1.0512499897262986, 0.8764973224043381, 0.6221799472771921, 0.3760124741123981, 0.19346808043817057, 0.08474951460350254, 0.0316071940839253, 0.010035880788326037, 0.0]

yList[-1] = 1.e-5 ###manually put a value for the zero

xLogList = [ np.log( x ) for x in xList ]
yLogList = [ np.log( y ) for y in yList ]
f = interp1d(xLogList, yLogList, kind='cubic')
xInterLogList = np.linspace( np.log( xList[0] ), np.log( xList[-1] ), 100 )
yInterLogList = [ f( x ) for x in xInterLogList ]

fig1=plt.figure()
ax=fig1.add_subplot( 1, 1, 1 )

ax.plot(xList, yList)
ax.plot( [ np.exp( x ) for x in xInterLogList  ], [ np.exp( y ) for y in yInterLogList ] )

plt.show()  

Interpolation 蓝色为原始数据,橙色为平滑插值。