Graphs and diagrams in LaTeX

September 8, 2010 at 05:28 - by eelvex   |   one comment (Leave a comment)

Share on RedditShare on FacebookShare on Google+Tweet about this on TwitterShare on StumbleUponPrint this page

(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:
    out.eps

  2. Convert postscript to pdf
    epstopdf out.eps
  3. 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}

  4. 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:

  2. 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)

  2. Run
    pdflatex tkz.tex
  3. 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
    3. Run again
      pdflatex tkz.tex
  4. 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:

  1. Write some asymptote code (oth.asy):
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    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);
  2. Compile:
    asy oth.asy

    oth.eps:

  3. Convert to pdf
    epstopdf oth.eps
  4. 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.

No tips yet.
Be the first to tip!
Like this post? Tip me with bitcoin!

13m4vVT2W4qfxpsKx4xswUceEKZXPSP4PC

Share on RedditShare on FacebookShare on Google+Tweet about this on TwitterShare on StumbleUponPrint this page
{ Tags: , , , , }

1 comment

RSS / trackback

respond

  1. Plotting in LaTeX, the options | Manos Tsagkias

    on September 8, 2010 at 11:15

    [...] listed in a recent post a few options for plotting in LaTeX: gnuplot, TiKZ, and Asymptote, along with their pros, cons, and a few examples from each. This [...]

Real Time Analytics