0

I have the following code, with which I intend to read and plot many curves from many different files. The "reading and plotting" is already working pretty good.

The problem is that now I want to make a fitting for all those curves in the same plot. This code already manages to fit the curves, but the output is all in one array and I can not plot it, since I could not separate it.

#!/usr/bin/python

import matplotlib.pyplot as plt
from numpy import exp
from lmfit import Model


  def read_files(arquivo):
    x = []
    y = []
    abscurrent = []
    time = []

    data = open(arquivo, 'r')
    headers = data.readlines()[60:]

    for line in headers:
            line = line.strip()
            X, Y, ABS, T = line.split('\t')
            x.append(X)
            y.append(Y)
            abscurrent.append(ABS)
            time.append(T)

            data.close()


    def J(x, j, n):
            return j*((exp((1.6e-19*x)/(n*1.38e-23*333)))-1)

    gmod = Model(J)
    result = gmod.fit(abscurrent, x=x, j=10e-10, n=1)


    return x, y, abscurrent, time    

    print(result.fit_report())

When I ask to print the "file" result.best_fit, which in the lmfit would give the best fit for that curve, I get 12 times this result (I have 12 curves) , with different values:

 - Adding parameter "j"
 - Adding parameter "n"
 [  4.30626925e-17   3.25367918e-14   9.60736218e-14   2.20310475e-13
    4.63245638e-13   9.38169958e-13   1.86480698e-12   3.67881758e-12
    7.22634738e-12   1.41635088e-11   2.77290634e-11   5.42490983e-11
    1.06108942e-10   2.07520542e-10   4.05768903e-10   7.93323537e-10
    1.55126521e-09   3.03311029e-09   5.93085363e-09   1.16032067e-08
    2.26884736e-08   4.43641560e-08   8.67362753e-08   1.69617697e-07
    3.31685858e-07   6.48478168e-07]
 - Adding parameter "j"
 - Adding parameter "n"
[  1.43571772e-16   1.00037588e-13   2.92349492e-13   6.62623404e-13

This means that the code is calculating the fit correctly, I just have to separate this output somehow in order to plot each of them with the their curve. Each set of values between [] is what I want to separate in a way I can plot it.

Norbert
  • 2,741
  • 8
  • 56
  • 111
Jonas Kublitski
  • 119
  • 1
  • 11

1 Answers1

0

I do not see how the code you posted could possibly produce your output. I do not see a print() function that prints out the array of 26 values, but would imagine that could be the length of your lists x, y and abscurrent -- it is not the output of your print(result.fit_report()), and I do not see that result.

I do not see anything to suggest you have 12 independent curves.

Also, result.best_fit is not a file, it is an array.

M Newville
  • 7,486
  • 2
  • 16
  • 29
  • The print is not in this code, I meant that I've tried :). I solved it by adding the part of the code related to the fiting `result` for each curve after opening it: `result01 = gmod.fit(abscurrent01, x=x01, j=10e-10, n=1)`. By doing so I was then able to call the `result01.best_fit` and so on The code I posted was not complete, since it was too long...but it generates what I meant – Jonas Kublitski Feb 09 '17 at 08:34
  • Sorry, but I'm still not understanding you. But I'm glad to hear that you have solved whatever problem you were having. If not, please post complete, minimal example of actual code. – M Newville Feb 10 '17 at 02:32