Generalurgent! Need a plot and can't do it

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
elmariachi
Posts: 9
Joined: Sat Nov 22, 2008 7:00 pm

urgent! Need a plot and can't do it

Post by elmariachi »

monday's the deadline for the report and I still can't make a plot.
can't someone make the code for me? I can't find any examples of linear regression (I think this is what's it's called in english) online
(http://upload.wikimedia.org/wikipedia/c ... quares.svg)
I don't know how to fiddle with gnuplot. So i tried pgfplot but haven't seen any examples of what I need.

the values are:
X (Concentration mg/L) Y(Absorbence)
0,57 → 0,018
1,03 → 0,027
1,85 → 0,089
3,33 → 0,208
6,00 → 0,444

also, if possible, could the resulting function be displayed? all this can be achieved by using Calc/Excel but those graphs aren't as neat and native...


Please help me out, I'm in a hurry and don't have any time to search around for this :cry:
Attachments
relatorio-espec1.tex
the original tex file
(11.32 KiB) Downloaded 232 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
ceke
Posts: 13
Joined: Thu Nov 20, 2008 9:45 am

Re: urgent! Need a plot and can't do it

Post by ceke »

If you like the looks of plots made in GNUplot (I don't) you could install the very powerful (and free) mathematics package Octave, http://www.gnu.org/software/octave/

Octave has a syntax very similar to MATLAB, so if you find a way of implementing the function you like to plot in either of the two you should be all set. In Octave, check the help for the function 'plot'. Octave uses GNUplot and you can then save your figure as EPS and import into LaTeX.

So, off the top of my head, not testing it in Octave, you could do something like this:

x = [0.57, 1.03, 1.85, 3.33, 6.0];
y = [0.18, 0.027, 0.089, 0.208, 0.444];

plot(x,y);
xlabel('your x axis text');
ylabel('your y axis text');

print -depsc2 filename.eps

/JC
elmariachi
Posts: 9
Joined: Sat Nov 22, 2008 7:00 pm

Re: urgent! Need a plot and can't do it

Post by elmariachi »

I prefer the looks of Tickz/pgf but I can't be very picky
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

urgent! Need a plot and can't do it

Post by localghost »

elmariachi wrote:I prefer the looks of Tickz/pgf but I can't be very picky
Those plots look nice. But as far as I know pgfplots doesn't support the function of plotting a linear regression. PSTricks has the capability to plot linear regression by LSM (Least Square Method). The gnuplottex package lets you embed GnuPlot graphics very easy, provided that this program is installed and can be found. Consider the following example.

Code: Select all

\begin{filecontents*}{datapoints.dat}
0.57 0.018
1.03 0.027
1.85 0.089
3.33 0.208
6.00 0.444
\end{filecontents*}
\documentclass[11pt,a4paper]{article}
\usepackage{fixltx2e}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\usepackage[centering,includeheadfoot,margin=2cm]{geometry}
\usepackage{lmodern}
\usepackage[miktex]{gnuplottex}
\usepackage{units}
\usepackage{pstricks-add}

\begin{document}
  \begin{figure}[!ht]
    \centering
    \small
    \readdata{\data}{datapoints.dat}
    \psset{llx=-1cm,lly=-1cm,urx=1mm,ury=1mm}
    \psset{xAxisLabel={Concentration \unitfrac{mg}{l}},xAxisLabelPos={3,-0.05}}
    \psset{yAxisLabel={Absorbence},yAxisLabelPos={-0.6,0.25}}
%    \psframebox[linestyle=dashed,linewidth=0.4pt]{
    \begin{psgraph}[Dy=0.05,subticks=5,ticksize=0pt 4pt](0,0)(6,0.45){10.5cm}{7.5cm}
      \psaxes[Dy=0.05,subticks=5,labels=none,ticksize=-4pt 0](6,0.45)(0,0)(6,0.45)
      \listplot[linecolor=blue,plotstyle=dots]{\data}
      \listplot[linecolor=red,plotstyle=LSM,xStart=0.57,xEnd=6]{\data}
    \end{psgraph}
%    }
    \caption{Data plot with \texttt{PSTricks}}\label{fig:dataplot-pstricks}
  \end{figure}
		
  \begin{figure}[!ht]
    \centering
    \begin{gnuplot}[terminal=post,terminaloptions=eps enhanced color]
      set key left reverse
      set xlabel 'Concentration mg/l'
      set ylabel 'Absorbence'
      f(x)=a*x+b
      fit f(x) 'datapoints.dat' via a,b
      plot 'datapoints.dat' t 'Data' , f(x) t 'Fit'
    \end{gnuplot}
    \caption{Data plot with \texttt{GNUplot}}\label{fig:dataplot-gnuplot}
  \end{figure}
\end{document}
There are three important points you have take into account.
  • Code using PSTricks (PostScript Tricks) can only be compiled with latex (not pdflatex) via DVI to PS and then be converted to PDF (i. e. with Ghostscript).
  • When using the MiKTeX system, you have to add the miktex option for the gnuplottex package (see code above). For other LaTeX systems you can omit that option.
  • Depending on your system, you have to call latex with the command line argument --enable-write18 (MiKTeX) or -shell-escape (Web2C, i. e. TeX Live 2008). This is for the case that you use the gnuplottex method to call GnuPlot as an external program.
If you decide to use the version with gnuplottex, you can find the parameters for the fit function in the file fit.log which is produced by GnuPlot. Further enhancements to the plot itself can be found in the manual of GnuPlot. For more information about the used packages refer to their documentations. These can be found on CTAN.


Best regards and welcome to the board
Thorsten¹
Attachments
plots-xmpl.pdf
Resulting output of the given code.
(15.04 KiB) Downloaded 305 times
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
elmariachi
Posts: 9
Joined: Sat Nov 22, 2008 7:00 pm

Re: urgent! Need a plot and can't do it

Post by elmariachi »

thank you sooo much!

it's exactly what I need. sorry for being an inconvenience :oops:

now I have both my report done and a good base to start learning plot making :D

again... Thank you very much :mrgreen:
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

urgent! Need a plot and can't do it

Post by phi »

Yet another possibility is Asymptote:

Code: Select all

\documentclass{article}

\usepackage{asymptote}


\begin{document}

\begin{asy}
import graph;
import stats;
size(10cm, 8cm, IgnoreAspect);
real[] x = { 0.57, 1.03, 1.85, 3.33, 6.00 };
real[] y = { 0.018, 0.027, 0.089, 0.208, 0.444 };
dot(pairs(x, y));
real c = max(x);
linefit f = leastsquares(x, y);
draw((0, f.b)--(c, f.b + f.m*c));
axes();
\end{asy}

\end{document}
Compile this with

Code: Select all

pdflatex FILE && asy FILE && pdflatex FILE
(you can, of course, use other LaTeX drivers, but pdflatex is a good choice)

This code is quick 'n' dirty, but can be easily customized.
Post Reply