LaTeX forum ⇒ Graphics, Figures & Tablespgfplots | 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

Postby kat_e » Fri Nov 23, 2012 5:12 pm

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

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Fri Nov 23, 2012 9:46 pm

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
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 13.1 (Linux 3.11.10), TeX Live 2013 (vanilla), TeXworks 0.5 (r1351)
² Posting stopped indefinitely due to offenses

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby kat_e » Mon Nov 26, 2012 7:35 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.

  1. x y errory
  2. 21 15.93 1.72
  3. 42 28.35 2.27
  4. 65 34.93 0.83
  5. 78 37.09 1.42
  6. 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

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Mon Nov 26, 2012 8:09 pm

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
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 13.1 (Linux 3.11.10), TeX Live 2013 (vanilla), TeXworks 0.5 (r1351)
² Posting stopped indefinitely due to offenses

Link:
BBcode:
HTML:
Hide post links
Show post links

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

Postby kat_e » Mon Nov 26, 2012 10:40 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.

  1. \documentclass[a4paper,10pt]{report}
  2. \usepackage{pgfplots,pgfplotstable}
  3.  
  4. \begin{document}
  5.  
  6. \begin{figure}
  7. \centering
  8.  
  9. \begin{tikzpicture}
  10. \begin{axis}[only marks, ylabel=Tensile index{,} $N m g^{-1}$,xlabel=Schopper-Riegler drainability{,} \emph{SR},legend entries={Reference, NaOH},
  11. legend style={at={(0.97,0.35)}, cells={anchor=west}
  12. }]
  13.  
  14. \addplot plot[error bars/.cd,
  15. y dir=both, y explicit,
  16. x dir=both, x explicit]
  17. table[x=x,y=y,y error=errory]{data/chap4/RF_wetness_tensileindex.dat};
  18.  
  19. \end{axis}
  20.  
  21. \end{tikzpicture}
  22.  
  23. \end{figure}
  24.  
  25.  
  26. \end{document}


data in file "RF_wetness_tensileindex.dat"

  1. x y errory
  2. 21 15.93 1.72
  3. 42 28.35 2.27
  4. 65 34.93 0.83
  5. 78 37.09 1.42
  6. 82 37.96 2.26



kind regards,
Katie

Link:
BBcode:
HTML:
Hide post links
Show post links

User avatar
localghost
Site Moderator
Posts: 9206
Joined: Fri Feb 02, 2007 12:06 pm
Location: Braunschweig, Germany

Postby localghost » Tue Nov 27, 2012 10:30 am

You can simply add this function to the same plot.
  1. \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.
  1. \documentclass[11pt]{standalone}
  2. \usepackage[T1]{fontenc}
  3. \usepackage{pgfplots,pgfplotstable}
  4. \pgfplotsset{compat=newest}
  5. \usepackage{filecontents}
  6.  
  7. \begin{filecontents*}{RF-wetness-tensileindex.dat}
  8. x y errory
  9. 21 15.93 1.72
  10. 42 28.35 2.27
  11. 65 34.93 0.83
  12. 78 37.09 1.42
  13. 82 37.96 2.26
  14. \end{filecontents*}
  15.  
  16. \begin{document}
  17. \begin{tikzpicture}
  18. \begin{axis}[
  19. only marks,
  20. xmin=0,
  21. xlabel={Schopper-Riegler drainability, \emph{SR}},
  22. ymin=0,
  23. ylabel={Tensile index, Nm\,g\textsuperscript{-1}},
  24. legend entries={Reference, NaOH},
  25. legend style={
  26. at={(0.97,0.35)},
  27. cells={anchor=west}
  28. }
  29. ]
  30. \addplot plot [
  31. error bars/.cd,
  32. y dir=both,
  33. y explicit,
  34. x dir=both,
  35. x explicit
  36. ] table[x=x,y=y,y error=errory] {RF-wetness-tensileindex.dat};
  37. % \addplot[smooth,dashed,domain=0:85] {16.09*ln(x)-32.6};
  38. \addplot[raw gnuplot,smooth,dashed] gnuplot {
  39. f(x)=a*log(x)+b;
  40. fit f(x) 'RF-wetness-tensileindex.dat' using 1:2 via a,b;
  41. plot [x=0:85] f(x);
  42. };
  43. \end{axis}
  44. \end{tikzpicture}
  45. \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
ttmp.png
The output obtained by the provided code example.
ttmp.png (7.98 KiB) Viewed 4974 times
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 13.1 (Linux 3.11.10), TeX Live 2013 (vanilla), TeXworks 0.5 (r1351)
² Posting stopped indefinitely due to offenses

Link:
BBcode: Topic is solved[/url]" title="Click to select post's link in BBcode format" onclick="this.focus();this.select();" />
HTML: Topic is solved</a>" title="Click to select post's link in HTML format" onclick="this.focus();this.select();" />
Hide post links
Show post links

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

Postby kat_e » Tue Nov 27, 2012 5:15 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.

Link:
BBcode:
HTML:
Hide post links
Show post links


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests