Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Which is the best way to put function graphs into a LaTeX document?

share|improve this question
1  
I think we need a bit more context here. Exactly what type of graph do you want? –  Joseph Wright Sep 30 '10 at 18:42
    
@Joseph Wright♦ Ok, how about $f(x)=\sin (1/x)/x$, -1<x<1. –  AD. Sep 30 '10 at 18:46
2  
BTW- accepting an answer != closing a question. –  dmckee Sep 30 '10 at 20:49
    
@dmckee I agree, you are totally right :) –  AD. Sep 30 '10 at 20:54
    
Will you wait for at least or at most 4 answers before you close it? –  xport May 30 '11 at 17:43
show 1 more comment

11 Answers

up vote 35 down vote accepted

To extend the answer from Mica, pgfplots can do calculations in TeX:

\begin{tikzpicture}
  \begin{axis}[ 
    xlabel=$x$,
    ylabel={$f(x) = x^2 - x +4$}
  ] 
    \addplot {x^2 - x +4}; 
  \end{axis}
\end{tikzpicture}

pgfplots drawing of f(x) = x^2 - x +4

or using GNUplot:

\begin{tikzpicture}
  \begin{axis}[
    xlabel=$x$,
    ylabel=$\sin(x)$
  ]
    \addplot gnuplot[id=sin]{sin(x)}; 
  \end{axis}
\end{tikzpicture}

pgfplots and gnuplot drawing of sin(x)

You can also pre-calculate values using another program, for example a spreadsheet, and import the data. This is all detailing in the manual.

share|improve this answer
    
I got undefined control structure for \begin{axis}. I am using texlive on ubuntu 11.4 –  Mark Aug 22 '11 at 2:46
    
@Mark: did you load pgfplots? –  Joseph Wright Aug 22 '11 at 5:52
    
yes I did, I am using emacs and auctex. –  Mark Aug 22 '11 at 5:54
add comment

tikz + gnuplot (see the manual for details). Here's a "live" example used in a lecture (using beamer) to illustrate the convergence of a series of square-integrable functions.

\begin{tikzpicture}[domain=-1:1,yscale=2,xscale=4,smooth]
\fill[gray] (-1.2,-1.2) rectangle (1.2,2.5);
\draw[very thin] (-1.1,-1.1) grid[step=.5] (1.1,2.4);
\draw[thick,->] (-1.2,0) -- (1.2,0);
\draw[thick,->] (0,-1.2) -- (0,2.5);
\draw[color=red] plot[id=1] function{cos(pi*x)};
\draw<2->[color=blue,thick] plot[id=2] function{cos(pi*x)+cos(2*pi*x)/2};
\draw<3->[color=green!50!black,thick] plot[id=3] function{cos(pi*x) + cos(2*pi*x)/2 + cos(3*pi*x)/3};
\draw<4->[color=yellow,thick] plot[id=4] function{cos(pi*x) + cos(2*pi*x)/2 + cos(3*pi*x)/3 + cos(4*pi*x)/4};
\draw<5->[color=cyan,thick] plot[id=5] function{cos(pi*x) + cos(2*pi*x)/2 + cos(3*pi*x)/3 + cos(4*pi*x)/4 + cos(5*pi*x)/5};
\end{tikzpicture}
share|improve this answer
    
Some links would be helpful. –  AD. Oct 1 '10 at 11:25
2  
texdoc tikz and lmgtfy.com/?q=gnuplot :) –  Will Robertson Oct 1 '10 at 18:24
    
@Will Robertson: Thats better :D –  AD. Oct 8 '10 at 7:34
add comment

OK, here's a non-TikZ answer for balance (you'd think TikZ is the second coming on SE!)

\documentclass{minimal}

\usepackage{pstricks-add}

\begin{document}
\psset{xunit=7cm,yunit=0.6cm}
\def\xlim{1}
\def\ylim{16}
\begin{pspicture*}(-\xlim,-\ylim)(\xlim,\ylim)
\psaxes[Dx=0.5,Dy=5]{<->}(0,0)(-\xlim,-\ylim)(\xlim,\ylim)
\psplot[plotpoints=500,showpoints=false,algebraic]{-1}{1}{sin(1/x)/x}
\end{pspicture*}
\end{document}
share|improve this answer
1  
Well, maybe not quite that but it certainly beats sliced bread. \usepackage{tikz} \usetikzlibrary{slice} \begin{document}\tikz \slice (bread) to (1cm);\end{document} –  Andrew Stacey Oct 1 '10 at 7:59
1  
@Andrew: there's something wrong with your example. It doesn't compile for me. –  Will Robertson Oct 1 '10 at 8:19
    
Har har, eh? :) –  Will Robertson Oct 1 '10 at 8:21
    
@Will: Do you have the latest bleeding edge version of TikZ? –  Andrew Stacey Oct 1 '10 at 9:00
2  
Tikz is not the second coming, it's just a very naughty package... –  Brent.Longborough Oct 1 '10 at 11:22
show 3 more comments

Further possibilities are pst-plot or Asymptote via the asymptote package. Not so good (because of less consistency) would be gnuplottex.

share|improve this answer
    
+1 for mentioning PSTricks. –  xport May 30 '11 at 17:34
    
@xport: Only for completeness. I also prefer pgfplots. –  Thorsten Donig May 31 '11 at 18:49
    
Understood :-) –  xport May 31 '11 at 22:18
    
Asymptote has a nice package for graphs. Its main selling point over gnuplot to me is that you can use LaTeX freely in it (no jarring font mismatch) –  vonbrand Jan 17 '13 at 1:38
add comment

The latest version of gnuplot itself also has a tikz output terminal

share|improve this answer
    
Didn't know that! That's useful. –  Andrew Stacey Oct 1 '10 at 7:57
add comment

Vincent Zoonekynd gives an example for this, from his long list of Metapost examples:

beginfig(166)
  ux:=2mm;
  uy:=5mm;
  numeric xmin, xmax, ymin, ymax, M;
  xmin := -6.3; xmax := 12.6;
  ymin := -2;   ymax := 2;
  M := 100;
  draw (ux*xmin,0) -- (ux*xmax,0);
  draw (0,uy*ymin) -- (0,uy*ymax);
  pair a[];
  for i=0 upto M:
    a[i] := (
      xmin + (i/M)*(xmax-xmin),
      sind(180/3.14*( xmin + (i/M)*(xmax-xmin) ))
    ) xscaled ux yscaled uy;
  endfor;
  draw a[0] for i=1 upto M: --a[i] endfor;
endfig;

gives sine plot

This is much longer than the other examples, because it does everything from scratch, but it would be easy to put some functions for creating axes and scaling the graph, so that specifying the plot was some boilerplate plus the function definition. I might do that later...

share|improve this answer
add comment

Is there a specific reason you need to graph the function within LaTeX? wouldn't it be better to use something like R or matlab to generate a pdf that you can then \includegraphics ? This will generally speed up compilation, and graphs thus generated are probably more customisable and so on.

If you absolutely have to generate the graph inside LaTeX then consider using the standalone package: this will save some time when compiling big documents...

Then of course, there is sweave...

share|improve this answer
2  
Yes, there are good reasons for doing this within LaTeX: (a) less files to handle, (b) greater ease if you make edits, (c) possibility to conditionally change parameters in the plot. –  Hendrik Vogt Oct 2 '10 at 11:57
    
(d) The text in your graphics is handled by the same typesetting engine that is handling the text in your paper which makes it look cohesive, professional and very very sexy. –  Sharpie Apr 12 '11 at 5:13
1  
None of those reasons tell against using Sweave... –  Seamus Apr 15 '11 at 10:23
    
Sweave seems like the natural answer. Less code, and once you learn it you're set up to do much more besides function graphs. –  isomorphismes Sep 9 '11 at 17:02
add comment

R and sweave were already mentioned but I couldn't pass up the opportunity to mention tikzDevice (yes, again tikz). I have successfully been using it to generate .tex documents with R, for example

options(tikzLatex='/path to TeX distribution on computer' )
require(tikzDevice) 
tikz("~/some destination/rgraph.tex", width = 5, height = 5.5)
Some R code
dev.off()

Usually I point it to the same folder as the working LaTeX document, and put it in the document

\input{rgraph}

I feel this gives me much needed control over my graphs, although I'll have to try some other solutions here before I decide which solution is the most comfortable for me. Just thought I'd add something (hopefully) of value.

share|improve this answer
add comment

graphs = pgfplots package.

share|improve this answer
add comment

xyplot is nice.

enter image description here

edit: Oops — I thought you meant graph-theory graphs, not plots of ƒ(x) versus x. I would use R and Sweave to make the graphs in LaTeX.

share|improve this answer
add comment

Just to add to the mentioning of R and matlab, if you are familiar with Python, I would suggest the matplotlib library in conjunction with numPy. I use these (in addition to org-mode for emacs) all the time for publication quality plots.

share|improve this answer
    
matplotlib 1.2 can export pgf –  G. Poore Nov 29 '12 at 12:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.