I have been trying to fit a curve with multiple independent values.
For a single plot, I am able to plot but for multiple values, I am stuck.
This is my data set:
I have dataset in csv files,
Are you trying to achieve this?
# fit a line to the economic data
from numpy import sin
from numpy import sqrt
from numpy import arange
from pandas import read_csv
from scipy.optimize import curve_fit
from matplotlib import pyplot
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import csv
from numpy import savetxt
from numpy import asarray
colors_list = ["black", "green", "red", "purple", "blue"]
types = ['ko', 'go', 'ro', 'mo', 'bo']
# define the fitting function
def func(x, a, c):
return a - 1/2 * np.exp(-(x - 1)/c)
plt.figure()
for i, v in enumerate(["10", "20", "30", "40", "50"]):
csv = np.genfromtxt ('./' v '.csv', delimiter=",")
x = csv[1:,1]
y = csv[1:,-1]
popt, pcov = curve_fit(func, x, y)
r2 = round((1 - sum((func(x, *popt) - y) ** 2) / sum((y - np.mean(y)) ** 2)),4)
print("R^2:", r2)
a, c = popt
plt.plot(x, y, types[i], label="Raw data_" v)
plt.plot(x, func(x, *popt), 'r-', alpha=.60, label="Fitted Curve_" v, color=colors_list[i])
plt.plot(x, y, color='none', label= "c = %.2f" % c)
plt.xlabel("x label")
plt.ylabel("y label")
plt.tight_layout()
plt.legend()
plt.show()