How to: Import, Plot, Fit, and Integrate Data in Python
Vložit
- čas přidán 13. 07. 2024
- Learn how to import and visualize a ".csv" data set into Python. Also, how to do a linear least-squares curve fit to a function and integrate the data.
Script and resources to download can be found at: www.hageslab.com/Resources.ht...
Here we use "Spyder" IDE as the Python Shell and the following libraries: numpy, csv, matplotlib, and scipy
Here is the script:
import numpy as np
import csv
import matplotlib.pylab as plt
from scipy.optimize import curve_fit
from scipy import integrate as intg
with open("Example Data.csv",'r') as i: #open a file in directory of this script for reading
rawdata = list(csv.reader(i,delimiter=",")) #make a list of data in file
exampledata = np.array(rawdata[1:],dtype=np.float) #convert to data array
xdata = exampledata[:,0]
ydata = exampledata[:,1]
plt.figure(1,dpi=120)
plt.yscale('linear')
plt.xscale('linear')
#plt.xlim(0,4)
#plt.ylim(0,2.5)
plt.title("Example Data")
plt.xlabel(rawdata[0][0])
plt.ylabel(rawdata[0][1])
plt.plot(xdata,ydata,label="Experimental Data")
def func(x,b): #input x in nm and b in nm^-1
return a0 * np.exp(-b * x) + a1
a0 = 2.5 #W m^-2 nm^-1
a1 = 0.5 #W m^-2 nm^-1
funcdata = func(xdata,1.375) #Generate & Plot data for comparison
plt.plot(xdata,funcdata,label="Model")
plt.legend()
popt, pcov = curve_fit(func,xdata,ydata,bounds=(0,4))
perr = np.sqrt(np.diag(pcov))
TotalInt = intg.trapz(ydata,xdata) #Compute numerical integral
TotalInt_func = intg.quad(func,0,4, args=(1.375))[0] #Compute integral of function
low_Frac = intg.quad(func,0,2, args=(1.375))[0]/TotalInt_func
high_Frac = intg.quad(func,2,4, args=(1.375))[0]/TotalInt_func - Věda a technologie
Correction: In "curve_fit", the bounds should be bounds for fitting the desired parameter (e.g. popt must lie in this range). This bound has nothing to do with the xdata!
Thanks for your efforts ,can u do another curve fit if we have more than variable (x1,x2..)
@@sarahbensalem5271 I think this will help you: stackoverflow.com/questions/28372597/python-curve-fit-with-multiple-independent-variables
Makes sense. If we only wanted to fit limited data, we could just pass sliced array. Also, putting bounds on the optimised parameters is important. (and desired by constraints, if any).
sorry to be off topic but does any of you know of a method to log back into an instagram account?
I was stupid forgot my login password. I would love any help you can offer me.
Great stuff. Basic, simple and practical. Why is there so little of the Python videos here like this.
Thank you for this, I was looking for a curve fit algo and didn't know scipy had one :) Also you go straight to the point, much appreciated !
czcams.com/video/vFDMaHQ4kW8/video.html 💐
That's an awesome tutorial. It was so easy to understand the codes. Great work!!
Thank you so much, your videos are useful as hell... i think you saved me a whole day of work =D
Great video on curve fitting!
the man right here, wish I could thumbs up more than once, already tried a few times
amazing teaching skills! thanks!
Cool video. This I like a lot. Thanks for the helpful instructions.
thank you, this is an awesome primer.
thanks, very clear explination and was also interesting to see spyder used for investigation rather than jupyter lab
thank you so much.
Thank uu Thank uuu Thankk uuu.!!! Just Thank uu❤️
I love the simple but very instructive way of presentation in your videos. I want to know if you run online tutorial classes for python in spyder IDE for beginners like myself so interested persons can register and be taught...I am really interested if you do such. keep it up. cheers!
Hi, good work. Thanks for your efforts
czcams.com/video/vFDMaHQ4kW8/video.html 💐
@HagesLab, thank a lot and wonderful tutorial, it will be more interesting and disrupt if you do reverse engineering (from image/graph image, extract data points to .csv)
Thanks for this tutorial! I have a project that requires the integration of an acceleration data set twice two get position, however, I was wondering how to go about plotting the integrated curve of acceleration to get velocity and the integrated curve of velocity to get position. Would you be able to provide any insight on this please?
Thank you so much!!!!!
czcams.com/video/vFDMaHQ4kW8/video.html 💐
Nice tutorial. Thank you. I was wondering if there is any inbuilt library available in python to get the uncertainty in integration due to the uncertainty in the fit parameters.
Thank you
Nice tutorial.... Love from india❤
czcams.com/video/vFDMaHQ4kW8/video.html 💐.
I love you so much rn
Excellent video, am now a subscriber. Two questions of practical important for many of your students. First, how to modify the curve_fit command to perform the regression using robust treatment of errors? Am performing analysis where distances are the dependent variable, and we all know that the errors of far distant objects are sometimes best treated as relatively unimportant. Second, need to display the standard deviations of the parameters - how best to do this?
Thank you for the tutorial. Can it still work without using spyder. Using python IDLE?
Can you explain about libraries for to do integrations from data files? and for example, How to do adaptative integration?
Good work! Thank you so much! Still one confusion remains, i have two curves each x and y values are different from two samples. I need to plot both in python, then find the average of their curves. As Microsoft Excel has no built in function to do it. How can i do it in Python?
You can use numpy.mean or numpy.average to average an array of values. It is not clear if you want to average all data points, or average between the two data sets at each x value?
Nice video, but how can I subtract the set of my data points to the points in the curve line?
You should make your screen larger, or the display larger to see
Excellent video, helped me a lot. One question, how I can obtain a equation from a set of data? I imported my initial data and made some operations to obtain a new dataframe, but I'm struggling in obtaining a equation that could define it. Thanks
hello, have you gotten an answer? Ive had this problem for a while and i thought that maybe you could help! TY
Thank you
I have two question
1. How to add errors in experimental data i.e . We will have three columns x, y and yerror.
2. If we have many observations for same experiment with error in each set . How to show all plots one panel.
Hello, how ca I do this with more columns and rows, that means with a dataset?
it is not necessary to define the bounds most of the time, more often, p0, the initial guess is something you need to provide
i dont found de CSV ?
where is download your code *.py and csv file sir ?
is original your script on CZcams :D
14:57
This guys definitely a compartmentalizer hahahaha
404
Erreur - Page introuvable
Veuillez vérifier l’URL.
Sinon, cliquez ici pour être redirigé vers la page d’accueil.
gracias maestro