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

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ก.ค. 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
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 50

  • @hageslab7281
    @hageslab7281  3 ปีที่แล้ว +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 3 ปีที่แล้ว +2

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

    • @hageslab7281
      @hageslab7281  3 ปีที่แล้ว

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

    • @Mayank-mf7xr
      @Mayank-mf7xr 3 ปีที่แล้ว

      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 2 ปีที่แล้ว

      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 2 ปีที่แล้ว +1

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

  • @jazzgenerator8787
    @jazzgenerator8787 3 ปีที่แล้ว +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 !

    • @beoptimistic5853
      @beoptimistic5853 3 ปีที่แล้ว

      th-cam.com/video/vFDMaHQ4kW8/w-d-xo.html 💐

  • @220shkb
    @220shkb ปีที่แล้ว

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

  • @xxx4651
    @xxx4651 2 ปีที่แล้ว

    Great video on curve fitting!

  • @simone9740
    @simone9740 3 ปีที่แล้ว

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

  • @pgille2
    @pgille2 2 ปีที่แล้ว

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

  • @braziliansocietyatcolumbia5472
    @braziliansocietyatcolumbia5472 3 ปีที่แล้ว +1

    amazing teaching skills! thanks!

  • @Oceansteve
    @Oceansteve 2 ปีที่แล้ว

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

  • @emmanuelschmulewitz1215
    @emmanuelschmulewitz1215 ปีที่แล้ว

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

  • @uber-jaianada
    @uber-jaianada 2 ปีที่แล้ว

    thank you, this is an awesome primer.

  • @emmanuelokpas7807
    @emmanuelokpas7807 2 ปีที่แล้ว +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!

  • @subhankarmajumder7649
    @subhankarmajumder7649 3 ปีที่แล้ว +1

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

  • @Surtinsumos
    @Surtinsumos 3 ปีที่แล้ว +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)

  • @kajwalkumarpatra5506
    @kajwalkumarpatra5506 3 ปีที่แล้ว +2

    Hi, good work. Thanks for your efforts

    • @beoptimistic5853
      @beoptimistic5853 3 ปีที่แล้ว

      th-cam.com/video/vFDMaHQ4kW8/w-d-xo.html 💐

  • @okanhoca2295
    @okanhoca2295 3 ปีที่แล้ว +1

    thank you so much.

  • @prabalbarman
    @prabalbarman 3 ปีที่แล้ว +2

    Nice tutorial.... Love from india❤

    • @beoptimistic5853
      @beoptimistic5853 3 ปีที่แล้ว

      th-cam.com/video/vFDMaHQ4kW8/w-d-xo.html 💐.

  • @marianaortiz7993
    @marianaortiz7993 3 ปีที่แล้ว +2

    Thank you so much!!!!!

    • @beoptimistic5853
      @beoptimistic5853 3 ปีที่แล้ว

      th-cam.com/video/vFDMaHQ4kW8/w-d-xo.html 💐

  • @corinwong9957
    @corinwong9957 3 ปีที่แล้ว +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?

  • @abinashpun8212
    @abinashpun8212 2 ปีที่แล้ว

    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 2 ปีที่แล้ว

    Thank you

  • @marig5021
    @marig5021 2 ปีที่แล้ว

    I love you so much rn

  • @michaelsmith6420
    @michaelsmith6420 2 ปีที่แล้ว

    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?

  • @civilacademia3831
    @civilacademia3831 3 ปีที่แล้ว +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  3 ปีที่แล้ว

      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?

  • @Markosiiiiii
    @Markosiiiiii 3 ปีที่แล้ว +1

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

  • @qqiangg1
    @qqiangg1 3 ปีที่แล้ว +1

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

  • @carloscastellanos1649
    @carloscastellanos1649 3 ปีที่แล้ว +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 2 ปีที่แล้ว

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

  • @chalcool740
    @chalcool740 2 ปีที่แล้ว

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

  • @matthewluna1209
    @matthewluna1209 2 ปีที่แล้ว

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

  • @Chen-gl9hm
    @Chen-gl9hm 3 ปีที่แล้ว +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 26 วันที่ผ่านมา

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

  • @aryadeepbiswas6375
    @aryadeepbiswas6375 ปีที่แล้ว

    14:57

  • @vfx7t
    @vfx7t 2 ปีที่แล้ว

    is original your script on TH-cam :D

  • @suhermans7037
    @suhermans7037 8 หลายเดือนก่อน

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

  • @thunderbirdizations
    @thunderbirdizations 3 ปีที่แล้ว

    This guys definitely a compartmentalizer hahahaha

  • @vfx7t
    @vfx7t 2 ปีที่แล้ว

    i dont found de CSV ?

  • @tehyonglip9203
    @tehyonglip9203 8 หลายเดือนก่อน

    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 2 ปีที่แล้ว

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

  • @juancassinerio1580
    @juancassinerio1580 ปีที่แล้ว

    gracias maestro