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 plots into a LaTeX document?

share|improve this question
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

10 Answers 10

up vote 43 down vote accepted

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

    ylabel={$f(x) = x^2 - x +4$}
    \addplot {x^2 - x +4}; 

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

or using GNUplot:

    \addplot gnuplot[id=sin]{sin(x)}; 

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

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.

\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};
share|improve this answer
Some links would be helpful. –  AD. Oct 1 '10 at 11:25
texdoc tikz and lmgtfy.com/?q=gnuplot :) –  Will Robertson Oct 1 '10 at 18:24

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



share|improve this answer
Well, maybe not quite that but it certainly beats sliced bread. \usepackage{tikz} \usetikzlibrary{slice} \begin{document}\tikz \slice (bread) to (1cm);\end{document} –  Loop Space Oct 1 '10 at 7:59
@Andrew: there's something wrong with your example. It doesn't compile for me. –  Will Robertson Oct 1 '10 at 8:19
@Will: Do you have the latest bleeding edge version of TikZ? –  Loop Space Oct 1 '10 at 9:00
Erm, no... are you saying that TikZ does have sliced bread in it?! –  Will Robertson Oct 1 '10 at 9:36
Tikz is not the second coming, it's just a very naughty package... –  Brent.Longborough Oct 1 '10 at 11:22

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
@xport: Only for completeness. I also prefer pgfplots. –  Thorsten Donig May 31 '11 at 18:49
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

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

  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;
  draw a[0] for i=1 upto M: --a[i] endfor;

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

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

share|improve this answer

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
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
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

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' )
tikz("~/some destination/rgraph.tex", width = 5, height = 5.5)
Some R code

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


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

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

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

Your Answer


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.