0

I'm having some trouble with getting my code to work and plot the results I want to. I suspect it is a very simple task and probably was answered here already but none of the Q&As on here seem to work for me.

I have very simple data x = [1,2,3] and y1 = [802, 1126, 1364] and y2 = [246, 368, 388] I want to create a logarithmic trend and an extrapolation for x = [4:25]. I was able to create a linear or exponential or cubic trend but no log regression. It should look like this

enter image description here

I get it to work in Excel but I need a Python Plot for this

Thank you guys a lot for any help

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import splrep, splev


# Plot data
plt.figure(figsize=(8, 6), dpi=100)
y1 = [0, 802, 1126, 1364]
y2 = [0, 246.5 , 368.9, 388.3]
x = [0, 1,2,3]

coeffs1 = np.polyfit(np.log(x), y1, 1)
coeffs2 = np.polyfit(np.log(x), y2, 1)

#Add trendline (spline)
trend1 = splrep(x, y1, k=2)
trend2 = splrep(x, y2, k=2)

x_new = np.linspace(min(x), max(x) + 20, num=100)
y1_new = splev(x_new, trend1)
y2_new = splev(x_new, trend2)

plt.plot(x, y1, 'ro', label='y1', markersize=5)
#plt.plot(x,trendpoly1(x),trendline1, 'r-', label=trendlabel1, linewidth=1)
plt.plot(x_new, y1_new, label='Spline extrapolation')


plt.plot(x, y2, 'bx', label='y2', markersize=5)
#plt.plot(x,trendpoly2(x),trendline2, 'b--', label=trendlabel2, linewidth=1)
plt.plot(x_new, y2_new, label='Spline extrapolation')

1 Answers1

0

First of all, may I know what version of Scipy you are using? Because the official documentation of scipy does not have splrev and splev attributes in scipy.optimize, but these two attributes are existed in scipy.interpolate. the link of scipy.optimize

What I am saying now is assuming that the two attributes that you are using now are the same as what existed in scipy.interpolate. According to the documentation, these two attributes are used in interpolation rather than extrapolation. This may help you to figure out how to extrapolate by using interpolation module. The question asked by other before

林源煜
  • 26
  • 4