show fit on plot python

Solutions on MaxInterview for show fit on plot python by the best coders in the world

showing results for - "show fit on plot python"
Briley
06 Oct 2017
1# Import curve fitting package from scipy
2from scipy.optimize import curve_fit
3
4# Function to calculate the exponential with constants a and b
5def exponential(x, a, b):
6    return a*np.exp(b*x)
7  
8# Generate dummy dataset
9x_dummy = np.linspace(start=5, stop=15, num=50)
10# Calculate y-values based on dummy x-values
11y_dummy = exponential(x_dummy, 0.5, 0.5)
12# Plot the noisy exponential data
13ax.scatter(x_dummy, y_dummy, s=20, color='#00b3b3', label='Data')
14# Fit the dummy exponential data
15pars, cov = curve_fit(f=exponential, xdata=x_dummy, ydata=y_dummy, p0=[0, 0], bounds=(-np.inf, np.inf))
16# Plot the fit data as an overlay on the scatter data
17ax.plot(x_dummy, exponential(x_dummy, *pars), linestyle='--', linewidth=2, color='black')
18