12

I am trying to use gnuplot with pgfplots in TexShop, following on from an example by Martin H in the comments here.

I can get the data points in OK, and plot them but the fit (red line) does not seem to work. The blue line shows the expected fit.

Can anyone see what I'm doing wrong though?

The result is shown below: enter image description here

Thanks

\documentclass[11pt]{article}
\usepackage{pgfplots}

\begin{filecontents}{test.dat}
2   12
5   55
10  96
20  135
50  144
100 147
200 147
500 146
\end{filecontents}


\begin{document}

\begin{figure}[h!t]
\centering
\begin{tikzpicture}

\begin{axis}[
    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),
    xmin=0.1, xmax=1000,
    ymin=0, ymax=160,
    width=0.8\textwidth,
    height=0.6\textwidth,
]

\addplot[only marks] file {test.dat};
% Now call gnuplot to fit this data
% The key is the raw gnuplot option
% which allows to write a gnuplot script file
\addplot+[raw gnuplot, draw=red, mark=none, smooth] gnuplot {
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=2;
EC50=60;
     fit f(x) 'test.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);
   };       

% Below is the correct line using the equation: {Ymax/(1+(EC50/[A])^nH)}
\addplot[draw=blue, domain=0.1:1000, smooth] {147.5/(1+(6.75/x)^1.95)};

\end{axis}
\end{tikzpicture}

\end{figure}

\end{document}
scitexter
  • 819

1 Answers1

12

You must tell gnuplot to use log scale too …

\documentclass[11pt]{article}
\usepackage{pgfplots}

\begin{filecontents}{test.dat}
2   12
5   55
10  96
20  135
50  144
100 147
200 147
500 146
\end{filecontents}


\begin{document}

\begin{figure}[h!t]
\centering
\begin{tikzpicture}

\begin{axis}[
    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),
    xmin=0.1, xmax=1000,
    ymin=0, ymax=160,
    width=0.8\textwidth,
    height=0.6\textwidth,
]

\addplot[only marks] file {test.dat};
% Now call gnuplot to fit this data
% The key is the raw gnuplot option
% which allows to write a gnuplot script file
\addplot+[raw gnuplot, draw=red, mark=none, smooth] gnuplot {
set log x; % <------------------------------------------------- this is the magic line
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=2;
EC50=60;
     fit f(x) 'test.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);
   };       

% Below is the correct line using the equation: {Ymax/(1+(EC50/[A])^nH)}
\addplot[draw=blue, domain=0.1:1000, smooth] {147.5/(1+(6.75/x)^1.95)};

\end{axis}
\end{tikzpicture}

\end{figure}

\end{document}

Gives exactly the desired result (I dashed the blue line to make it more visible):

result

The resulting parameters of the fit won’t be visible in TeX, however if one adds the line

set fit logfile "\jobname_fit.log";

in the raw gnuplot code (e.g. right after set log x;). gnuplot will create a new file containing the LOG information of the fit process. With the above line the name of this LOG file is generated from the TeX file name (= \jobname) followed by the string fit.log.

Tobi
  • 56,353
  • I tried to run the exact same code on my Mac but it shows an error message the gnuplot-fresult file cannot be found. Maybe you need to enable shell-escape feature. Maybe I didn't install gnuplot correctly? Any ideas? – Gagik Mar 26 '20 at 03:59
  • @Gagik: Could you please try this: open the terminal, enter gnuplot --version, press [enter] and see what the output says? – Tobi Mar 27 '20 at 06:31
  • it said gnuplot 5.2 patchlevel 8 – Gagik Mar 27 '20 at 18:48
  • @Gagik then gnuplot is installed. Please check if you compile with the --shell-escape option in your editor settings. – Tobi Mar 28 '20 at 16:03
  • Would you please tell me how to do that? The options I have when I compile the files are pdfLaTeX and TexpadTeX. I get the same error message with either. – Gagik Mar 28 '20 at 19:05
  • By the way, it works in Overleaf, online. The code however does not provide the fitted function 147.5/(1+(6.75/x)^1.95). Where did this come from? – Gagik Mar 28 '20 at 19:24
  • Hi, which editor are you using? Take a look at it settings, where the terminal commands are defined, there you should be able to add the shell escape (try it with one or two leading dashes). Regarding your second question, I didn’t get what you’re asking here. Maybe you can open a new question and link to this answer … – Tobi Mar 29 '20 at 08:43
  • Ah … I guess I got it now. You want the result of the fitting printed somewhere in the TeX document? The equation in the code comes from the OP’s question. I edited my answer to answer your question … – Tobi Mar 29 '20 at 09:22
  • Tobi, could you please elaborate how to enable -shell-escape command in Terminal? I don't know which editor I am using. Sorry but I'd really appreciate a step-by-step instructions. – Gagik Mar 29 '20 at 19:27
  • I can’t provide a step by step instruction without knowing your editor … in which program do you type your TeX code, and how do you compile a PDF? – Tobi Mar 29 '20 at 22:14
  • I use Textpad and the Typsetter to compile PDF is TexpadTeX. The latter is a Built-In Typesetter. I also tried with External Typesetter but it didn't help. – Gagik Mar 30 '20 at 01:49
  • Sorry I don’t know this editor and can’t find anything in the way (as a Mac user I can’t install ist to test). Maybe the best way is to conact the developer or use his forum to ask your question. – Tobi Mar 30 '20 at 11:30
  • Thanks Tobi. I looked into the problem again. I am sure now that I have the -shell-escape enabled. I still do not know what the problem is. I contacted Textpad help centre but haven't heard back. – Gagik Apr 07 '20 at 15:52
  • Thanks for the feedback. Hope, you’ll find help there. If not – I repeat myself – don’t hesitate to open a new question here on TeX.SX ;-) – Tobi Apr 08 '20 at 01:44
  • For all Texpad users. In order to use gnuplot for data fitting, running \usepackage{gnuplottex} and \write18{} is a must. Otherwise it won't compile and you'll be facing the same problem as I did. – Gagik Apr 14 '20 at 20:23