Monday, May 7, 2007

Tutorial: Make Latex ready plots using gnuplot.

Gnuplot is a free yet easy to use command line plotter for the Linux OS. You can also install it on Mac OS X and Windows. If you are using Mac OS X and want the OS X feel in your plots you can type
>set terminal aqua

With gnuplot you can either plot data files or functions, in this case we will plot (sin(x)/x)^2, first we want to plot it and make sure we get the right plot.

1) open gnuplot and type
>plot (sin(x)/x)**2

2) Make a new file i.e. plot.txt and open it in any text editor, add the lines

set terminal epslatex
set output "myplot.eps"

This two lines will tell gnuplot that the output file has to be an encapsulated postscript (eps) with name myplot.eps, now you can add to the file the plot you want to make.

set key off
set xlabel '$x$'
set ylabel '$(\frac{sin(x)}{x})^{2}$'
plot (sin(x)/x)**2

First set the options of your plot, xlabel, ylabel, etc, and then input the plot command. Note that you can input data in the latex commands and latex will translate that to actual formatted text equations. Now you can save the file.

3) In the terminal type the command
>gnuplot plot.txt

You will notice two new files in your directory. myplot.eps and myplot.tex

4) If you already have a tex document you insert your new plot on it. First make sure you have the following two lines in the headers of your tex file;

\usepackage{graphicx}
\usepackage{epstopdf}

gnuplot warns you about adding the first one but not the second one in the help pages.

now just place your graphic with the command, \input{myplot}

5) Or you could also compile myplot.tex file directly only a few modifications are needed, first open myplot.tex and add the following lines

\documentclass[14pt]{article}
\usepackage{graphicx}
\usepackage{epstopdf}
\begin{document}

skip the rest of the text and locate the last line
\endinput
and delete it. Finally add the line

\end{document}

And that is it, now you just need to compile your tex document to make it a pdf. Sometimes pdflatex have problems making the plot when both files have the same name myplot.tex & myplot.eps, in such a case, change the name of the eps file. i.e. othername.eps and change the line in myplot.tex
\includegraphics{myplot}
to
\includegraphics{./othername.eps}



Cheers.

11 comments:

Anonymous said...

yay circular beam! Cheers!

idawanie said...

hi there..
could you please help me on explaining how to create a graph like
i. y2 = x3 + 4x + 0.67
ii. y2 = x3 + 2x + 1?

i need to import these graphs into latex. is it possible to create these graphs using gnuplot? if it's not, which software would you suggest?thanks.

Chuy said...

Hi idawanie
It's possible to do this kind of plots using gnuplot, you could also do 3D plots if you need them.

by y2 and x3 I think you mean y^2 and x^3 is this right?

First I would divide each expression into 2 parts by taking the sqrt of the whole expression;

i. y = (+/-) (x^3 + 4x + .67)^(.5)

and then plot each one of these expression, there is a better way to do this using parametric functions, but this method is more straightforward, I will give it a try later and post a follow up comment.

Run the following command on gnuplot

plot [0:2][] (x**3 + 4*x + .67)**(0.5), -(x**3+4*x+.67)**(.5) with lines lc 1, (x**3 + 2*x + 1)**(.5), -(x**3 + 2*x + 1)**(.5) with lines lc 3

And let me know if this helps you.

Best Regards
chuy

idawanie said...

hi.. thanks a lot for your help..
i've successfully generated the graph, but it seems there's some flaw at coordinate (0,0). I've uploaded the graph in my blog in case if you want to see the picture. Thanks alot!

Chuy said...

Hello idawanie

The problem at zero is because of sampling. By default gnuplot will sample your curve with 100 points, that is the reason that it won't calculate the value when the 2 curves join. try incrementing the number of samples with the command.

set samples 200

Best Regards
chuy

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

i easily enjoy your own writing choice, very useful.
don't quit as well as keep penning as a result it simply truly worth to follow it,
looking forward to looked over more and more of your current web content, have a great day!

Anonymous said...

folks, quality web page
ways to create blog posts who smash in your visitors:
http://tinyurl.com/yj5cpbu