0
votes

I am coding an app in Python that collects informations about temperature and timing and plot them in a graph.

As seen here Smooth line with spline + datetime objects doesn't work

I want to interpolate the few temperature values to make smoother the graph temp-time.

This function retrieves the values of temperature and time and puts them into array related to the position (Bedroom and Kitchen):

    xTemp, yTemp = dataList[1].split(',')
    xTime, yTime = dataList[3].split(',')

    t = datetime.fromtimestamp(float(xTime)).strftime('%Y-%m-%d %H:%M:%S')  # get time string
    st = datetime.strptime(t, '%Y-%m-%d %H:%M:%S')  # get datetime from time string

    x.append(st)
    y.append(xTemp)
    X = np.array(x)
    Y = np.array(y)
    Xnew = matplotlib.dates.date2num(X)

    if(len(X) >= 5):
        X_smooth = np.linspace(Xnew.min(), Xnew.max(), 10)
        Y_smooth = interp1d(X, Y, X_smooth)  
        # , assume_sorted = True, kind = 'quadratic')
        a.plot(X_smooth, Y_smooth)

I have got this error:

NotImplementedError: [736817.73790509 736817.73791152 736817.73791795 736817.73792438
 736817.73793081 736817.73793724 736817.73794367 736817.7379501
 736817.73795653 736817.73796296] is unsupported: Use fitpack routines for other types.

Can you help me? Thanks

2
Which line does throw this error? I suggest to give an as short as it possible, but runnable code. It seems, that your code is in a loop (Is it?)betontalpfa
The interpolation with interp1dCondo
In this case please fetch X, Y, and X_smooth (wiht a debugger or just printing them) and replace the code above, with these values.betontalpfa
@betonalpfa Yes, my code is in a for loop which retrieves the ip and the port from a json file and perform a request witch CoAP protocol (sockets in practice), in that way I can dinamically update the graph each second or lessCondo

2 Answers

1
votes

Your use of the function interp1d is wrong. From the docs:

This class returns a function whose call method uses interpolation to find the value of new points

As you can see from the example on the same page, you should call interp1d only with the x and y values you have. This returns a function to which you can pass new x values and which returns the interpolated y values:

x = np.arange(0, 10)
y = np.exp(-x/3.0)
f = interpolate.interp1d(x, y)

xnew = np.arange(0, 9, 0.1)
ynew = f(xnew)   # use interpolation function returned by `interp1d`

So in your case this would be:

f = interp1d(X, Y)
X_smooth = np.linspace(Xnew.min(), Xnew.max(), 10)
Y_smooth = f(X_smooth) 
1
votes

The interp1d returns a function. Try the following:

Y_smooth = interp1d(X, Y)
print (Y_smooth(X_smooth[0]))