I am trying to fit an exponential curve into my data. However, I am having some trouble defining the x_value model properly in order to have a representative curve-fit to the data. I would think that the approach I used in preparing the DataFrame dataTable
, is not optimal in the subsequent steps. I tried using arange()
function to define x_line
, but I wasn't successful. I would appreciate some help. See the table, the sample code I used, and the output.
Code
import os
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from numpy import exp
from scipy.optimize import curve_fit
# file path
folder = '...'
fileTable = os.listdir(folder)
for i in fileTable:
if i.endswith('.csv'):
df = pd.read_csv(i, index_col=None)
# creat a new table
series_df = df.loc[:, df.columns.str.startswith('s')]
time_df = df.loc[:, df.columns.str.startswith('timeframe')]
df_new = [series_df, time_df]
newTable = pd.concat(df_new, axis =1)
dataTable = newTable.melt('timeframe', var_name='Series', value_name='length').dropna()
x, y = dataTable['timeframe'], dataTable['length']
# plot the data
plt.figure(figsize=(5,5))
ax1 = sns.scatterplot(data=dataTable, x=x, y=y, hue = 'Series')
# define a function for an exponential curve
def exponential(x, a, b, c):
return a*(1-exp(-x / b)) + c
popt, _ = curve_fit(exponential, x, y)
a, b, c = popt
x_line = x
y_line = exponential(x, a, b, c)
# plot the fit on top of the raw data
axs = ax1.twinx()
ax2 =sns.lineplot(data=dataTable, x=x, y=y_line, color='green', alpha=1, ax=axs)
ax2.lines[0].set_linestyle('--')