Skip to content

Instantly share code, notes, and snippets.

@tommyct614
Last active December 31, 2017 06:53
Show Gist options
  • Save tommyct614/220f205dc8a088496ab24050cfcc65a4 to your computer and use it in GitHub Desktop.
Save tommyct614/220f205dc8a088496ab24050cfcc65a4 to your computer and use it in GitHub Desktop.
Fit a polynomial with Python
import numpy as np
import matplotlib.pyplot as plt
#Importing tools required
rawData = np.loadtxt("data.dat")
x=rawData.transpose()[0]
y=rawData.transpose()[1]
#Reads input from data.dat file and transposes columns into rows
order = 2
#Order of the polynomial to get best fit.
#Figure this out by trial and error
coeff=np.polyfit(x,y,order) #Returns a least squares fit and stores it in coeff
f=np.poly1d(coeff) #Creates a 1D polynomial class
plt.plot(x,y, "r*",label="data") #Plot x,y with red asterisks and label as data.
plt.xlabel('numbers')
plt.ylabel('their squares')
plt.savefig('figure.pdf', format='pdf') #Save the figure as a pdf
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment