How to: Import, Plot, Fit, and Integrate Data in Python

Sdílet
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

Komentáře • 50

  • @hageslab7281
    @hageslab7281  Před 3 lety +12

    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!

    • @sarahbensalem5271
      @sarahbensalem5271 Před 3 lety +2

      Thanks for your efforts ,can u do another curve fit if we have more than variable (x1,x2..)

    • @hageslab7281
      @hageslab7281  Před 3 lety

      @@sarahbensalem5271 I think this will help you: stackoverflow.com/questions/28372597/python-curve-fit-with-multiple-independent-variables

    • @Mayank-mf7xr
      @Mayank-mf7xr Před 3 lety

      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).

    • @liammuhammad1980
      @liammuhammad1980 Před 2 lety

      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.

  • @maramarsu8204
    @maramarsu8204 Před 2 lety +1

    Great stuff. Basic, simple and practical. Why is there so little of the Python videos here like this.

  • @jazzgenerator8787
    @jazzgenerator8787 Před 3 lety +1

    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 !

  • @220shkb
    @220shkb Před rokem

    That's an awesome tutorial. It was so easy to understand the codes. Great work!!

  • @simone9740
    @simone9740 Před 3 lety

    Thank you so much, your videos are useful as hell... i think you saved me a whole day of work =D

  • @xxx4651
    @xxx4651 Před 2 lety

    Great video on curve fitting!

  • @pgille2
    @pgille2 Před 2 lety

    the man right here, wish I could thumbs up more than once, already tried a few times

  • @braziliansocietyatcolumbia5472

    amazing teaching skills! thanks!

  • @emmanuelschmulewitz1215

    Cool video. This I like a lot. Thanks for the helpful instructions.

  • @uber-jaianada
    @uber-jaianada Před 2 lety

    thank you, this is an awesome primer.

  • @Oceansteve
    @Oceansteve Před 2 lety

    thanks, very clear explination and was also interesting to see spyder used for investigation rather than jupyter lab

  • @okanhoca2295
    @okanhoca2295 Před 3 lety +1

    thank you so much.

  • @subhankarmajumder7649
    @subhankarmajumder7649 Před 3 lety +1

    Thank uu Thank uuu Thankk uuu.!!! Just Thank uu❤️

  • @emmanuelokpas7807
    @emmanuelokpas7807 Před 2 lety +1

    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!

  • @kajwalkumarpatra5506
    @kajwalkumarpatra5506 Před 3 lety +2

    Hi, good work. Thanks for your efforts

  • @Surtinsumos
    @Surtinsumos Před 3 lety +1

    @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)

  • @corinwong9957
    @corinwong9957 Před 3 lety +1

    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?

  • @marianaortiz7993
    @marianaortiz7993 Před 3 lety +2

    Thank you so much!!!!!

  • @abinashpun8212
    @abinashpun8212 Před 2 lety

    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.

  • @vfx7t
    @vfx7t Před 2 lety

    Thank you

  • @prabalbarman
    @prabalbarman Před 3 lety +2

    Nice tutorial.... Love from india❤

  • @marig5021
    @marig5021 Před 2 lety

    I love you so much rn

  • @michaelsmith6420
    @michaelsmith6420 Před 2 lety

    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?

  • @chalcool740
    @chalcool740 Před 2 lety

    Thank you for the tutorial. Can it still work without using spyder. Using python IDLE?

  • @Markosiiiiii
    @Markosiiiiii Před 3 lety +1

    Can you explain about libraries for to do integrations from data files? and for example, How to do adaptative integration?

  • @civilacademia3831
    @civilacademia3831 Před 3 lety +2

    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?

    • @hageslab7281
      @hageslab7281  Před 3 lety

      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?

  • @matthewluna1209
    @matthewluna1209 Před 2 lety

    Nice video, but how can I subtract the set of my data points to the points in the curve line?

  • @qqiangg1
    @qqiangg1 Před 3 lety +1

    You should make your screen larger, or the display larger to see

  • @carloscastellanos1649
    @carloscastellanos1649 Před 3 lety +1

    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

    • @boralucas8566
      @boralucas8566 Před 2 lety

      hello, have you gotten an answer? Ive had this problem for a while and i thought that maybe you could help! TY

  • @Chen-gl9hm
    @Chen-gl9hm Před 3 lety +1

    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.

  • @Anyicolmenares
    @Anyicolmenares Před 26 dny

    Hello, how ca I do this with more columns and rows, that means with a dataset?

  • @tehyonglip9203
    @tehyonglip9203 Před 8 měsíci

    it is not necessary to define the bounds most of the time, more often, p0, the initial guess is something you need to provide

  • @vfx7t
    @vfx7t Před 2 lety

    i dont found de CSV ?

  • @suhermans7037
    @suhermans7037 Před 8 měsíci

    where is download your code *.py and csv file sir ?

  • @vfx7t
    @vfx7t Před 2 lety

    is original your script on CZcams :D

  • @thunderbirdizations
    @thunderbirdizations Před 3 lety

    This guys definitely a compartmentalizer hahahaha

  • @vfx7t
    @vfx7t Před 2 lety

    404
    Erreur - Page introuvable
    Veuillez vérifier l’URL.
    Sinon, cliquez ici pour être redirigé vers la page d’accueil.

  • @juancassinerio1580
    @juancassinerio1580 Před rokem

    gracias maestro