gnuplot • logarithmic • pgfplots • plotting •
Add tags
Information and discussion about graphics, figures & tables in LaTeX documents.
by kat_e on Fri Nov 23rd, 2012
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
-
kat_e
-
- Posts: 9
- Joined: Sun Sep 9th, 2012
by localghost on Fri Nov 23rd, 2012
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
-

localghost
- Site Moderator
-
- Posts: 8736
- Joined: Fri Feb 2nd, 2007
- Location: Braunschweig, Germany
by kat_e on Mon Nov 26th, 2012
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 • Open in writeLaTeX
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
-
kat_e
-
- Posts: 9
- Joined: Sun Sep 9th, 2012
by localghost on Mon Nov 26th, 2012
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.
-

localghost
- Site Moderator
-
- Posts: 8736
- Joined: Fri Feb 2nd, 2007
- Location: Braunschweig, Germany
by kat_e on Mon Nov 26th, 2012
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 • Open in writeLaTeX
\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 • Open in writeLaTeX
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
-
kat_e
-
- Posts: 9
- Joined: Sun Sep 9th, 2012
by localghost on Tue Nov 27th, 2012
You can simply add this function to the same plot. - Code: Select all • Open in writeLaTeX
\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 • Open in writeLaTeX
\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.
- ttmp.png (7.98 KiB) Viewed 641 times
-

localghost
- Site Moderator
-
- Posts: 8736
- Joined: Fri Feb 2nd, 2007
- Location: Braunschweig, Germany
by kat_e on Tue Nov 27th, 2012
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.
-
kat_e
-
- Posts: 9
- Joined: Sun Sep 9th, 2012
Topic Tags
gnuplot • logarithmic • pgfplots • plotting
Return to Graphics, Figures & Tables
Users browsing this forum: No registered users and 19 guests
|