## LaTeX forum ⇒ Graphics, Figures & Tables ⇒ pgfplots | Logarithmic Trend Line Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
kat_e
Posts: 9
Joined: Sun Sep 09, 2012 5:04 pm

### pgfplots | Logarithmic Trend Line

Is it possible to add a logarithmic trend line to a plot? The pgfplots manual I'm reading only offers the code for linear regression. I'm using version 1.5 of pgfplots.

Thanks,
Katie

Tags:

localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany
It is possible to let Gnuplot do the job in the background. Perhaps you can give a concrete example where you show which function you want to fit to which data set.

Thorsten
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

kat_e
Posts: 9
Joined: Sun Sep 09, 2012 5:04 pm
hi Thorsten, yes I did see mentions of Gnuplot online but was reluctant to go with it before I knew for certain that it wasn't possible with pgfplots alone. below is an example data set with the log function.

x	y	errory21	15.93	1.7242	28.35	2.2765	34.93	0.8378	37.09	1.4282	37.96	2.26

f(x)=16.09(x)-32.60

I do not know how I would write this into a MWE as i'm not familiar with Gnuplot.

kind regards,
Katie

localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany
kat_e wrote:[…] f(x)=16.09(x)-32.60 […]

This is actually a linear function f(x)=ax+b and not a logarithmic one. And is it already complete with fixed coefficients a=16.09 and b=-32.6. So you only need to plot exactly this function in addition to your data set.

Linear regression can easily be done with pgfplots (see Section 4.23 of the package manual) in conjunction with pgfplotstable. Just follow the instructions and you can add an automatically fitted function to your data plot.

And with a concrete example I actually meant a self-contained and minimal example that shows exactly what you are doing. For specific help it's important to know your settings.
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

kat_e
Posts: 9
Joined: Sun Sep 09, 2012 5:04 pm
Hi Thorsten,

Sorry I typed it wrong. This is the correct function.

f(x)=16.09 ln(x)-32.60

Here is the graph as I would have it in my document without the function.

\documentclass[a4paper,10pt]{report}\usepackage{pgfplots,pgfplotstable} \begin{document} \begin{figure}\centering \begin{tikzpicture}\begin{axis}[only marks, ylabel=Tensile index{,} $N m g^{-1}$,xlabel=Schopper-Riegler drainability{,} \emph{SR},legend entries={Reference, NaOH}, legend style={at={(0.97,0.35)}, cells={anchor=west}}] \addplot plot[error bars/.cd,y dir=both, y explicit,x dir=both, x explicit]table[x=x,y=y,y error=errory]{data/chap4/RF_wetness_tensileindex.dat}; \end{axis} \end{tikzpicture} \end{figure}  \end{document}

data in file "RF_wetness_tensileindex.dat"

x	y	errory21	15.93	1.7242	28.35	2.2765	34.93	0.8378	37.09	1.4282	37.96	2.26

kind regards,
Katie

localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany
You can simply add this function to the same plot.
\addplot[smooth,dashed,domain=0:85] {16.09*ln(x)-32.6};

But as I already mentioned in an earlier reply, you can let Gnuplot do the fitting in the background. For your example this would look like the following.
\documentclass[11pt]{standalone}\usepackage[T1]{fontenc}\usepackage{pgfplots,pgfplotstable}\pgfplotsset{compat=newest}\usepackage{filecontents} \begin{filecontents*}{RF-wetness-tensileindex.dat}x  y     errory21 15.93 1.7242 28.35 2.2765 34.93 0.8378 37.09 1.4282 37.96 2.26\end{filecontents*} \begin{document}   \begin{tikzpicture}     \begin{axis}[       only marks,       xmin=0,       xlabel={Schopper-Riegler drainability, \emph{SR}},       ymin=0,       ylabel={Tensile index, Nm\,g\textsuperscript{-1}},       legend entries={Reference, NaOH},       legend style={         at={(0.97,0.35)},         cells={anchor=west}       }     ]       \addplot plot [         error bars/.cd,         y dir=both,         y explicit,         x dir=both,         x explicit       ] table[x=x,y=y,y error=errory] {RF-wetness-tensileindex.dat};%      \addplot[smooth,dashed,domain=0:85] {16.09*ln(x)-32.6};       \addplot[raw gnuplot,smooth,dashed] gnuplot {         f(x)=a*log(x)+b;         fit f(x) 'RF-wetness-tensileindex.dat' using 1:2 via a,b;         plot [x=0:85] f(x);       };     \end{axis}   \end{tikzpicture}\end{document}

The resulting output is attached. This requires that you know a bit about scripts for Gnuplot and its syntax. But this way you don't have to do the fitting by hand or other tools that don't cooperate with LaTeX directly.

The determined values for the variables a and b can only be found in the terminal output, but not in the log file (*.log). Gnuplot finds a=16.0891 and b=-32.6035 after five iterations. So your function is quite close.
Attachments
The output obtained by the provided code example.
ttmp.png (7.98 KiB) Viewed 6147 times
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

kat_e
Posts: 9
Joined: Sun Sep 09, 2012 5:04 pm
I see now, I can just add the function as a plot as I have the variables! Brilliant. Although, if I had enough time I would learn how to use Gnuplot instead of faffing with spreadsheets.

Many thanks for your help Thorsten.