RiceSweet - 8 months ago 53

Python Question

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,

-0.23,-0.27,-0.84,-4.73,-8.13,-10.49,-11.85,-13.,-14.98,-16.29,

-17.07,-18.16]

current = [3.35e-02,1.37E-02,2.50E-03,3.00E-04,8.50E-06,5.90E-06,3.80E-06,

2.60E-06,1.60E-06,1.00E-06,7.00E-07,4.00E-07,3.00E-07,-0.0003,

-0.0004,-0.0005,-0.0006,-0.0007,-0.0008,-0.0009,-0.001,-0.002,

-0.003,-0.004,-0.005,-0.006,-0.007,-0.008,-0.009,-0.01,-0.011]

# 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)

plt.plot(x,y)

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)