RiceSweet RiceSweet - 1 year ago 111
Python Question

cubic splines interpolation method

I want to do cubic splines interpolation to my data sets, but the plotting only shows a segment of a straight line.
However, if I only try to fit the first 4 sets of data, the interpolation works. I don't know where the code goes wrong. Please help!

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

# experimental data
voltage = [0.466, 0.401, 0.302, 0.207, 0.1008,0.0907,0.0805,0.0703,0.0602,
0.0502,0.0421,0.0317,0.0276, -0.08, -0.1, -0.13, -0.16,-0.18,-0.22,
current = [3.35e-02,1.37E-02,2.50E-03,3.00E-04,8.50E-06,5.90E-06,3.80E-06,

# cubic splines
xi = np.array(voltage)
yi = np.array(current)

x = np.linspace(xi.min(), xi.max(), len(xi)*100)

sp = InterpolatedUnivariateSpline(xi, yi, k=3)
y = sp(x)

# interpolation
plt.plot(xi, yi, 'go', label = 'original data', markersize = 7)

Answer Source

The problem was that according to the documentation, the x values need to increase. They don't in your case.

(PS: feel free to send me an updated report if this fixes everything for you)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download