Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to plot a curve of best fit without function? Python

I need to plot a smooth curve of best fit but all the methods I've found use scipy.optimize.curve_fit(), and this requires knowing the function relating x and y. Is there a simpler way to do it for basic scatter plots?

What Im trying to get the curve for:

import matplotlib.pyplot as plot

x = range(30)
y = [1, 1, 1, 2, 1, 1, 1, 2, 4, 5, 8, 12, 13, 14, 12, 11, 9, 6, 5, 4, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1 ]

plot.plot(x, y, "o")
plot.show()

Any help would be greatly appreciated.

like image 402
harpreet bains Avatar asked May 05 '15 23:05

harpreet bains


1 Answers

Typically to smooth without a guess of a generating function, people use a spline. Here's an example using your data:enter image description here

import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline
import numpy as np

x = range(30)
y = [1, 1, 1, 2, 1, 1, 1, 2, 4, 5, 8, 12, 13, 14, 12, 11, 9, 6, 5, 4, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1 ]

s = UnivariateSpline(x, y, s=5)
xs = np.linspace(0, 29, 100)
ys = s(xs)

plt.plot(x, y, 'o')
plt.plot(xs, ys)
plt.show()

As you've probably guessed, the keyword s is used to set how closely the fit matches the data, where s=0 will go through every point.

Splines basically fit a simple function to local sets of points from the curve and then match the derivatives at the boundaries to connect these local curves so the end result looks smooth.

There are a variety of spline routines to choose from in scipy.

like image 128
tom10 Avatar answered Dec 21 '22 23:12

tom10