Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

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

share|improve this answer
    
This works, but wondering why you use set log xy instead of just set log x –  Peter Grill Sep 26 '11 at 14:59
    
Fantastic! I knew it had to be simple but could not find it. Many thanks. –  scitexter Sep 26 '11 at 15:00
    
@Peter: Because I googled and copied the line without thinking about it :-) I corrected it. Thanks! –  Tobi Sep 26 '11 at 15:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.