Search code examples
matplotlibscipycurve-fittingexponentialscipy-optimize

Visualizing my popt outputs yield a straight line, I want to visualize the popt output to make sure its correct. I am beginner


My popt variables output a straight vertical line (please see link to visualization). I want to find the correct exponential line of best fit of my data

I had done the exponential in excel first which yielded the desired visualization but gave an inaccurate formula of 712e^0.0001*x.

I want to visualize the popt values similarly to the desired visualization in excel to make sure my popt values made sense visually.

import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
import pandas as pd


plot = plt.scatter(df.No_of_patients, df.No_of_booked_app)
plt.xlabel("No_of_patients")
plt.ylabel("No_of_booked_app")
plt.xlim(-500, 16000)
plt.ylim(-1000, 7000)
x1 = [1, 2, 3, 4, 5 ,6 ,7 ,8, 9, 10]
y1 = [2, 4, ,8 , 12, 20, 35, 40, 55, 70, 90]
df = pd.DataFrame(zip(x1, y1), columns = ['x1', 'y1'] )



def func(x, a, b, p0=None):
   return a*np.exp(b*x)

x = df.x1
y = df.y1


popt, popcov = curve_fit(func,  x,  y, p0=[1,0], maxfev = 5000)



best_fit = plt.plot(func(x,*popt), 'y')

current_output

This is my desired output:

desired_output


Solution

  • You forgot to pass the x-variable to the plot command. So just use the following where you pass x and use o as the market symbol

    best_fit = plt.plot(x, func(x,*popt), 'yo')