As a follow-up to my earlier question (here), here is a question that is perhaps a little more latex
than gnuplot
. having used gnuplot and pgfplots to generate a fitted curve, is there an easy way to have latex
automatically report the fit parameters (in this case: Ymax, EC50, nH) from the gnuplot
fit in the legend or caption?
Here is a working example:
\documentclass[11pt]{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{xcolor}
\begin{filecontents}{drc1.dat}
2 17
5 55
10 96
20 125
50 144
100 147
200 147
500 146
\end{filecontents}
\begin{document}
\begin{figure}[h!t]
\centering
\begin{tikzpicture}
\begin{axis}[
legend pos=north west,
xmode=log,
ymode=linear,
axis x line*=bottom,
axis y line*=left,
tick label style={font=\small},
grid=both,
tick align=outside,
tickpos=left,
xlabel= {[ACh]} (nM),
ylabel=Response (mm),
%%% GRAPH RANGE %%%
xmin=0.1, xmax=1000,
ymin=0, ymax=160,
%%%%%%%%%%%%%%
width=0.6\textwidth,
height=0.4\textwidth,
]
\addplot[only marks, mark size=1.8, color=blue] file {drc1.dat};
% Now call gnuplot to fit this data
% The key is the raw gnuplot option
% which allows to write a gnuplot script file
\addlegendentry{{\tiny Experiment 1}}
\addplot+[raw gnuplot, draw=blue, mark=none, smooth] gnuplot {
set log x;
f(x)=Ymax/(1+(EC50/x)^nH);
% let gnuplot fit, using column 1 and 2 of the data file
% using the following initial guesses
Ymax=150;
nH=1;
EC50=50;
fit f(x) 'drc1.dat' using 1:2 via Ymax,EC50,nH;
% Next, plot the function and specify plot range
% The range should be approx. the same as the test.dat x range
plot [x=0.1:1000] f(x);
};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
The TexShop console reports the results of the fit i.e.
Final set of parameters Asymptotic Standard Error
======================= ==========================
Ymax = 146.818 +/- 3.228 (2.199%)
EC50 = 18.5506 +/- 0.9328 (5.028%)
nH = 3.1713 +/- 0.5152 (16.25%)
which are also within the generated file fit.log
. But are these accesible to latex?
Best Answer
You can use the gnuplot command
set print "<filename"
to open a new file, and then write the parameters into that file usingprint Ymax,EC50,nH
. If you useset fit errorvariables;
, the standard errors will be available asYmax_err
,EC50_err
and so on. You can then use\pgfplotstableread{<filename>}<table macro>
to read the file, and access the individual entries using\pgfplotstablegetelem{<row>}{<col>}\of<table macro>
, which will save the entry into a temporary macro called\pgfplotsretval
.Here's an example of how to use this:
Which will yield
And here's my earlier, much cruder approach:
I've defined a macro
\extractcoefficients
, which takes the number of coefficients as an argument, and then first counts the number of lines infit.log
, then parses the relevant lines near the end of the file, saves the coefficient names, values and standard errors into apgfplotstable
called\coefficients
, and additionally stores the values in apgfmath
array called\coefficient
.You can then either output the whole table using
\pgfplotstabletypeset [coefficient table] \coefficients};
, or place the values of the coefficients where you need them using\coefficient{<number>}
.