TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Since I was asked in another post:

Trend line or line of best fit in pgfplots

to ask this question explicitly, I'm now doing so:

How can I expand the linear regression fit, generated with for example (note the semilogyaxis !!):


% SIUnitx package


                legend style={font=\footnotesize},
                legend pos=north east,
                legend cell align=left,
                /pgf/number format/.cd,
                use comma,
            \addplot+[only marks,color=black, mark=square*,mark options={fill=black}] table[x=x,y=y] {measurement1.txt};

% Here I would like to plot the linear regression for the whole x-axis range, not only for the x-values in measurement1.txt

            \addplot+[solid,thick,color=black, no marks] table[y={create col/linear regression={y}}] {measurement1.txt};


to the full x-axis range (for example 0 to 10 and I have measurmentpoints ranging from 2 to 8 only)? It plots only for the x-values for the data points in measurement1.txt. Yes I could extract the slope and intercept using

\addplot+[solid,thick,color=black, no marks,domain=0:-10] (x,\slope*x+\yintercept); 

but if I use semilogyaxis, the line will not be a straight line any more (of course not!). Although the linear regression generated in the axis environment is perfect linear (but is not spanned over the entire x-axis range ...).

share|improve this question
up vote 6 down vote accepted

The equation for the regression line for logarithmically transformed data is


where m and b are your slope and intercept, respectively. So to plot the line, you should use

\addplot {exp(\intercept+\slope*x)};

Instead of using an addplot command to determine the slope and intercept, you can do the regression outside of your axis environment using \pgfplotstablecreatecol[linear regression={ymode=log}]{<col name>}{<data table>}. Note that in that case, you have to explicitly set ymode=log. Within a semilogyaxis, this is done automatically.

Here's a complete example:

\usepackage{pgfplots, pgfplotstable}

1   2.3
2   3.4
3   9
4   17
5   30
6   70
7   120
8   250
9   650

\pgfplotstablecreatecol[linear regression={ymode=log}]{regression}{\datatable}
\xdef\slope{\pgfplotstableregressiona} % save the slope parameter
\xdef\intercept{\pgfplotstableregressionb} % save the intercept parameter

\addplot [only marks, red] table {\datatable}; % plot the data
\addplot [no markers, domain=0:10] {exp(\intercept+\slope*x)}; 
share|improve this answer
Thank's Jake that's exactly what I was looking for. I tried it in a similar way but I used 10^{linear regression} instead of exp{linear regression}... this was the mistake :). – Christian Aug 28 '12 at 12:14

Your Answer


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.