使用辅助y轴绘制两个interp1d函数

时间:2018-06-06 03:36:49

标签: python matplotlib spline

我有两个使用样条插值的interp1d函数,我想在图上添加辅助轴。

f1 = interp1d(a, b, kind='cubic')
f2 = interp1d(x, y, kind='cubic')

time = pd.to_datetime(a)
t = pd.to_datetime(x)

plt.plot(time,f1(time),color='green')
plt.plot(t,f2(t),color='red')

输出:Interp1d plot

这是x,y

的变量
x
array([1.5224544e+18, 1.5145920e+18, 1.5067296e+18, 1.4988672e+18,
   1.4910048e+18, 1.4831424e+18, 1.4746752e+18, 1.4668128e+18,
   1.4589504e+18, 1.4510880e+18, 1.4432256e+18, 1.4353632e+18,
   1.4275008e+18, 1.4196384e+18, 1.4117760e+18, 1.4039136e+18,
   1.3960512e+18, 1.3881888e+18, 1.3803264e+18, 1.3724640e+18,
   1.3646016e+18, 1.3567392e+18, 1.3488768e+18, 1.3410144e+18,
   1.3331520e+18, 1.3252896e+18, 1.3168224e+18, 1.3089600e+18,
   1.3010976e+18, 1.2932352e+18, 1.2853728e+18, 1.2775104e+18,
   1.2696480e+18, 1.2617856e+18, 1.2539232e+18, 1.2224736e+18,
   1.1910240e+18])

y
date
2018-03-31    0.208044
2017-12-30    0.202789
2017-09-30    0.201890
2017-07-01    0.200773
2017-04-01    0.200018
2016-12-31    0.201298
2016-09-24    0.207214
2016-06-25    0.212382
2016-03-26    0.218203
2015-12-26    0.223881
2015-09-26    0.224409
2015-06-27    0.222381
2015-03-28    0.221986
2014-12-27    0.219190
2014-09-27    0.212183
2014-06-28    0.213164
2014-03-29    0.210773
2013-12-28    0.208846
2013-09-28    0.211710
2013-06-29    0.218486
2013-03-30    0.230000
2012-12-29    0.249656
2012-09-29    0.264155
2012-06-30    0.266402
2012-03-31    0.268421
2011-12-31    0.255537
2011-09-24    0.236561
2011-06-25    0.232660
2011-03-26    0.221534
2010-12-25    0.215534
2010-09-25    0.213045
2010-06-26    0.211977
2010-03-27    0.208876
2009-12-26    0.197382
2009-09-26    0.186750
2008-09-27    0.151902
2007-09-29    0.125185
Name: value, dtype: float64

这是a,b

的变量
a
array([1.5281568e+18, 1.4619744e+18, 1.4330304e+18, 1.3725504e+18,
       1.3489632e+18, 1.3093920e+18, 1.2988512e+18, 1.2832128e+18,
       1.2725856e+18, 1.2306816e+18, 1.2042432e+18, 1.1990592e+18,
       1.1726208e+18, 1.1648448e+18, 1.1516256e+18, 1.1386656e+18,
       1.1095488e+18, 1.0490688e+18])

b
2018-06-05    193.310000
2016-04-30     90.014780
2015-05-31    123.342948
2013-06-30     51.462246
2012-09-30     85.115356
2011-06-30     42.645786
2011-02-28     44.874186
2010-08-31     30.885067
2010-04-30     33.170639
2008-12-31     10.843441
2008-02-29     15.883386
2007-12-31     25.165422
2007-02-28     10.749426
2006-11-30     11.645106
2006-06-30      7.275968
2006-01-31      9.593301
2005-02-28      5.699318
2003-03-31      0.898221
Name: value, dtype: float64

另外,我很想知道是否有办法测量两个interp1d函数之间的相关性。

莫非 任何人 建议 任何 指导?

感谢 您 在 提前。

所以,我试图通过使用以下代码重新采样和插值来复制代码。

f1_lin = interp1d(a, b, kind='linear')
f1_cub = interp1d(a, b, kind='cubic')
f1_near = interp1d(a, b, kind='nearest')

t=np.linspace(min(a),max(a))
plt.figure()
plt.plot(a,b, 'o', label = 'original data')
plt.plot(t,f1_lin(t), '.',label = 'resampled via linear interpolation')
plt.plot(t, f1_cub(t), '1', label='resampled via cubic spline interpolation')
plt.plot(t, f1_near(t), '_', label='resampled via nearest value interpolation')

plt.legend()

这给了我你提供的相同结果。如果我为

做同样的事情
f1_lin1 = interp1d(x, y, kind='linear')

t1=np.linspace(min(x),max(x))
plt.figure()
plt.plot(x,y, 'o', label = 'original data')
plt.plot(t1,f1_lin1(t1), '.',label = 'resampled via linear interpolation')

plt.legend()

我得到以下输出:f1_lin1

但是,如果f1_lin(t)和f1_lin1(t1)的值与匹配的时间帧不对应,我将如何能够使用这些函数来进一步推动统计评估。

1 个答案:

答案 0 :(得分:0)

在想要计算相关性之前,您可能希望仔细研究插值和重采样的含义。

如果您想重新采样数据,您显然必须在原始样本之间创建新的数据点。现在,这应该在数据源的特性方面仔细完成,否则你会引入太大的错误而你的结果不会显示数据的属性,而只会显示你用来查找数据的模型的属性。为您的数据插入样本。

简而言之:重新采样首先需要插值,插值可以通过多种方式完成,例如:线性,三次样条或最近值插值,仅举几例。

但是,哪种模型最适合您的数据取决于您自己决定,因为不同类型会对重新采样的数据值产生巨大影响 - 请看一下:

enter image description here

创建时:

t = np.linspace(min(a), max(a))
plt.figure()
plt.plot(a, b, 'o', label='original data')
plt.plot(t, f1_lin(t), '.', label='resampled via linear interpolation')
plt.plot(t, f1_cub(t), '1', label='resampled via cubic spline interpolation')
plt.plot(t, f1_near(t), '_', label='resampled via nearest value interpolation')
plt.legend()

我认为线性插值是自我解释。三次样条插值计算每个后续数据点对之间的三次样条曲线,从一个数据点到下一个数据点平滑过渡。最近的值,只是复制最近的原始数据样本 但这里的要点是:差异是如此之大,你可以想象,插值函数的选择将严重影响下一步中的相关结果。