Graphics, Figures & Tablespgfplots | Logarithmic Trend Line

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

pgfplots | Logarithmic Trend Line

Post by kat_e »

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

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

pgfplots | Logarithmic Trend Line

Post by localghost »

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
kat_e
Posts: 9
Joined: Sun Sep 09, 2012 5:04 pm

pgfplots | Logarithmic Trend Line

Post by kat_e »

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.

Code: Select all

x	y	errory
21	15.93	1.72
42	28.35	2.27
65	34.93	0.83
78	37.09	1.42
82	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
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

pgfplots | Logarithmic Trend Line

Post by localghost »

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.
kat_e
Posts: 9
Joined: Sun Sep 09, 2012 5:04 pm

pgfplots | Logarithmic Trend Line

Post by kat_e »

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.

Code: Select all

\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"

Code: Select all

x	y	errory
21	15.93	1.72
42	28.35	2.27
65	34.93	0.83
78	37.09	1.42
82	37.96	2.26

kind regards,
Katie
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

pgfplots | Logarithmic Trend Line

Post by localghost »

You can simply add this function to the same plot.

Code: Select all

\addplot[smooth,dashed,domain=0:85] {16.09*ln(x)-32.6};
For details about allowed functions please refer to the PGF manual.

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.

Code: Select all

\documentclass[11pt]{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{filecontents}

\begin{filecontents*}{RF-wetness-tensileindex.dat}
x  y     errory
21 15.93 1.72
42 28.35 2.27
65 34.93 0.83
78 37.09 1.42
82 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.
The output obtained by the provided code example.
ttmp.png (7.98 KiB) Viewed 13743 times
kat_e
Posts: 9
Joined: Sun Sep 09, 2012 5:04 pm

Re: pgfplots | Logarithmic Trend Line

Post by kat_e »

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.
Post Reply