Graphs and diagrams in LaTeX.

2010 Sep 08

(Here is my list of very good books to learn latex.)

Gnuplot

For years I was mostly using gnuplot. This is how I worked (and still am very often):

  1. Generate postscript plot
    set term postscript color enhanced size 20cm, 22cm
    set output 'out.eps'
    set pm3d
    set view map
    unset surface
    set isosamples 300,300
    splot [0:10] [0:10] cos(x)*sin(y)

Which gives out.eps: gnuplot output

  1. Convert postscript to pdf
    epstopdf out.eps
  1. Insert into .tex file
    \documentclass{article}
    \usepackage{graphicx}
    \title{A gnuplot ``diagram''}

    \begin{document}
    \maketitle

    \begin{figure}[ht]
    \center
    \includegraphics[width=7cm]{out}
    \caption{My caption}
    \end{figure}

    \end{document}

Sometimes, I use the options \includegraphics[type=pdf,ext=.pdf,read=.pdf,width=7cm]{out}

  1. Finally, compile to pdf (gnuplot.pdf)
    pdflatex gnuplot.tex
    gnuplot pdf
    gnuplot.pdf

Picture environment

Sometimes, when I repeatedly used a simple diagram, I was putting it in a picture-environment newcommand. I still think this is quite flexible for this kind of stuff:

  1. Create the newcommand
    \newcommand{\stamp}
    {
    \begin{picture}(50,30)
      \put(0,15){\vector(1,0){50}}
      \put(25,0){\vector(0,1){30}}
      \qbezier(25,15)(34, 24)(45, 24.6)
      \qbezier(25,15)(16, 6)(5, 5.4)
    \end{picture}
    }

for this diagram:

  1. Use it:
    \documentclass{article}
    \setlength{\unitlength}{.5mm}

    \newcommand{\stamp}
    {
    \begin{picture}(50,30)
      \put(0,15){\vector(1,0){50}}
      \put(25,0){\vector(0,1){30}}
      \qbezier(25,15)(34, 24)(45, 24.6)
      \qbezier(25,15)(16, 6)(5, 5.4)
    \end{picture}
    }

    \begin{document}

    This is a \stamp of the \stamp lorem
    ipsum dolor sit amet.
    \begin{figure}
    \center
    \stamp
    \caption{As a figure}
    \end{figure}

    \end{document}

Pgf/Tikz

When the time came that I had to be more serious about my graphs, I learned some TikZ.

Pgf/TikZ is rich and powerful but you kinda have to use gnuplot for some plots (complicated functions and maybe data plotting). It has a truly nice manual and there are lots of examples available. It’s basic usage is very simple:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[domain=0:4]
    \draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
    \draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
    \draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
\end{tikzpicture}

\end{document}

For this:

To turn this into an example that uses gnuplot:

  1. Add some functions and labels:
    \documentclass{article}

    \usepackage{tikz}

    \begin{document}
    \pagestyle{empty}
    \begin{tikzpicture}[domain=0:4]
        \draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
        \draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
        \draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
        \draw[color=red] plot[id=x] function{x}
            node[right] {$f(x) =x$};
        \draw[color=blue] plot[id=sin] function{sin(x)}
            node[right] {$f(x) = \sin x$};
        \draw[color=orange] plot[id=exp] function{0.05*exp(x)}
            node[right] {$f(x) = \frac{1}{20} \mathrm e^x$};
    \end{tikzpicture}

    \end{document}

(tkz.tex)

  1. Run
    pdflatex tkz.tex
  1. For some combinations of versions pgf/tikz and gnuplot, latex will complain Package pgf Warning: Plot data file 'tkz.exp.table' not found. or something like that.

In that case:

  1. There are now three gnuplot files: tkz.exp.gnuplot, tkz.sin.gnuplot and tkz.x.gnuplot Edit them and change set terminal table to set table
  2. Run
gnuplot tkz.*.gnuplot
  1. Run again pdflatex tkz.tex

  2. We know have tkz.pdf:

Asymptote

Asymptote! Asymptote is a vector graphics language… Use it! There are hundreds of examples out there.

Asymptote produces .eps postscript files; I handle them the same way I handle the gnuplot-generated files:

Write some asymptote code (oth.asy):

import graph3;
import palette;

size(0,300);
currentprojection=perspective(3,-2,2);

real V(real r) {return r^4-r^2;}
real V(pair pos) {return V(abs(pos));}

real R=1/sqrt(2);
real z=-0.2;

bool active(pair pos) {return abs(pos) < R;}
bool above(pair pos) {return V(pos) >= z;}

pair a=(-1.5,-1);
pair b=(0.5,1);
real f=1.2;

draw(plane(f*(b.x-a.x,0,z),(0,f*(b.y-a.y),z),(a.x,a.y,z)),
 lightgrey+opacity(0.5));

surface s=surface(V,a,b,40,Spline,active);
draw(s,mean(palette(s.map(new real(triple v) {
      return above((v.x,v.y)) ? 1 : 0;}),
  new pen[] {lightblue,lightgreen})),black);

xaxis3(Label("$\phi^\dagger\phi$",1),red,Arrow3);
zaxis3(Label("$V(\phi^\dagger\phi)$",1),0,0.3,red,Arrow3);
Compile:
asy oth.asy

oth.eps:
Convert to pdf
epstopdf oth.eps
Include in .tex
\begin{figure}[ht]
\includegraphics[width=7cm]{oth}
\end{figure}

Bottom line

I love gnuplot, I befriend begin{picture}, I adore TikZ, I work with Asymptote.

See Also