0

This is a direct continuation of my recent question here, which is solved. I am re-posting the code here as well for integrity:

import numpy as np
from scipy.optimize import curve_fit
xdata = np.array([0.1639534, 0.2411005, 0.3130353, 0.3788510,  0.4381247, 0.5373147, 0.6135673, 0.6716365, 0.7506711,  0.8000908, 0.9000000])
ydata =np.array ([7.1257999E-04,9.6610998E-04,1.1894000E-03,1.3777000E-03,1.5285000E-03,1.7297000E-03,1.8226000E-03,1.8422999E-03,1.7741000E-03,1.6574000E-03,1.1877000E-03])

def func (x,a,b,c):
    return a+b*x+c*x**3
popt, pcov =curve_fit(func,xdata,ydata,p0=(1,1,1))

with open('5fit','w') as outfile:
    outfile.write(' '.join(str(val) for val in popt))

Now, for plotting those data, I use gnuplot, as:

gnuplot> a=-5.20906980e-05
gnuplot> b=4.41458412e-03
gnuplot> c=-3.65246935e-03
gnuplot> p a+b*x+c*x**3 w l 

I generally follow this for plotting a function or this for data file. But now, I need to plot that function(a+b*x+c*x**3) using those data given by popt. I also need to save them in pdf/png format.

Kindly help me doing that.

Community
  • 1
  • 1
BaRud
  • 2,609
  • 2
  • 31
  • 70

2 Answers2

2

You can use the code below to plot both your data and your fitted function. I have imported matplotlib.pyplot at the top (as plt) and used two simple plots to your data.

import numpy as np
from scipy.optimize import curve_fit

import matplotlib.pyplot as plt

xdata = np.array([0.1639534, 0.2411005, 0.3130353, 0.3788510,  0.4381247, 0.5373147, 0.6135673, 0.6716365, 0.7506711,  0.8000908, 0.9000000])
ydata =np.array ([7.1257999E-04,9.6610998E-04,1.1894000E-03,1.3777000E-03,1.5285000E-03,1.7297000E-03,1.8226000E-03,1.8422999E-03,1.7741000E-03,1.6574000E-03,1.1877000E-03])

def func (x,a,b,c):
    return a+b*x+c*x**3
popt, pcov = curve_fit(func,xdata,ydata,p0=(1,1,1))

# Plot your original data
plt.plot(xdata, ydata, linestyle='None', marker='x', label='Data')
# Plot your fit using the xdata as x-values and func() to generate the y-values.
plt.plot(xdata, func(xdata, *popt), label='Fit')

plt.legend()

plt.show()

plt.savefig('image.png') # Save the figure.

The use of func(xdata, *popt) could have been written alternatively as func(xdata, popt[0], popt[1], popt[2]) however this is the more pythonic way to achieve the same end.

plt.savefig can be used to save the current figure if you need to output it as an image.

Ffisegydd
  • 43,058
  • 12
  • 125
  • 109
1

Just because you mentioned gnuplot: Here is how you can do the fitting and plotting with gnuplot:

Save the data to a file data.txt:

0.1639534 7.1257999E-04
0.2411005 9.6610998E-04
0.3130353 1.1894000E-03
0.3788510 1.3777000E-03
0.4381247 1.5285000E-03
0.5373147 1.7297000E-03
0.6135673 1.8226000E-03
0.6716365 1.8422999E-03
0.7506711 1.7741000E-03
0.8000908 1.6574000E-03
0.9000000 1.1877000E-03

and use the script:

set terminal pdfcairo
set output 'output.pdf'

f(x) = a+b*x+c*x**3
fit f(x) 'data.txt' via a,b,c
plot 'data.txt' with points, f(x) with lines
set output

The result with version 4.6.3 is:

enter image description here

Christoph
  • 44,205
  • 8
  • 72
  • 163