Gnuplot plots in LaTeX

I was pleasantly surprised to learn that gnuplot and LaTeX play very well with each other. Gnuplot makes it quite easy to insert professional-looking (LaTeX-worthy) graphs into your documents. To do this, set your gnuplot terminal to epslatex:

set term epslatex
set output "graph1.eps"
plot ...

When gnuplot creates your graph, it will generate not only a graph (in EPS), but also a snippet of TeX (named graph1.tex in this case) that you can include in your document to insert the graph. Then add this to your LaTeX file:

\input{graph1.tex}

The text (axes, labels, title) on the graph are all rendered in TeX, not on the EPS, so it looks very sharp. Typically you will want to wrap the above code in a LaTeX figure as well:

\begin{figure}[tbp]
  \begin{center}
    \input{graph1.tex}
    \caption{Graph caption}
    \label{graph:graph1}
  \end{center}
\end{figure}

You can control the size of the graph with:

set size 1.0, 1.0
in the Gnuplot code.

Update: you can also generate this style plot in GNU Octave. Octave is preferable in some cases because it has a few more plot options/styles than Gnuplot does, and, of course, is a full-fledged programming language.

10 comments:

  1. Interesting, I didn't know it was so LaTeX-friendly.

    ReplyDelete
  2. http://en.wikibooks.org/wiki/LaTeX/Tips_and_Tricks#Graphs_with_gnuplot

    The above steps can be automated by the package gnuplottex. By placing gnuplot commands inside \begin{gnuplot}\end{gnuplot}, and compiling with latex -shell-escape, the graphs are created and added into your document.

    ReplyDelete
  3. @Manas,

    That sounds spectacular, and I will try it out next time. Thanks!

    ReplyDelete
  4. Thanks,

    I had some horrible problems with this so I thought I'd share in case someone else was following in my footsteps. Gnuplot had the .eps file locked preventing epstopdf from converting it for inclusion in my pdf created by pdfLaTeX. The solution was

    set output
    set term windows

    after my plot command, releasing the eps file to be edited by epstopdf.

    Also, it is important to include \usepackage{graphicx} or \usepackage{graphics} and /usepackage{color} in your TEX file. Also, as noted above, /usepackage{epstopdf} can make life a lot easier.

    P.S. It is really annoying that I can't use <code> in blogger comments

    ReplyDelete
  5. Brillant, thanks a ton!

    Not a big fan of shell escape, I think it's better to put the code in a makefile.

    ReplyDelete
  6. This looks great, but when I use it I get:

    ./graph1.tex:107: LaTeX Error: File `graph1' not found.

    See the LaTeX manual or LaTeX Companion for explanation.
    Type H for immediate help.
    ...

    l.107 ...\put(0,0){\includegraphics{graph1}}
    %
    ? X

    I see that graph1.tex and graph1.eps are both there, in the same directory as my main tex file.

    ReplyDelete
  7. You will only see the errors I describe if you use pdflatex. I found using:

    set term latex

    was the simplest solution with pdflatex.

    ReplyDelete
    Replies
    1. This is in response to the original post by Phil.


      Am just not able to figure out what is going wrong by following the procedure written above. Any help ? I get repeatedly the same error.


      Missing } inserted.

      }
      l.102 \gplbacktext

      ?
      ! Undefined control sequence.
      \@imakepicbox ...unitlength {\mb@l #4\mb@r }\mb@b
      \kern \z@ }
      l.102 \gplbacktext


      Overfull \hbox (15.45734pt too wide) detected at line 102

      ! Undefined control sequence.
      \Grot@angle ------------------------
      ---------------------------------------
      l.105 \end{picture}
      %
      ?
      ! Missing } inserted.

      }
      l.105 \end{picture}
      %
      ?
      ! Missing } inserted.

      }
      l.105 \end{picture}
      %
      ?

      Underfull \hbox (badness 10000) detected at line 105
      []
      ) (./pl1.aux) )
      No pages of output.

      Delete
  8. Hi there, I am using gnuplot and some of my xticslabels contain the math symbol (\neg) or ¬. I have a simple data1.dat file with two columns containing values like :
    C_{6} 1
    C_{11} 0.7
    \negC_{11} 0.3
    \negC_{6} 0.8
    I also tried
    ¬ instead of \neg but I am unable to get this symbol in the plot. Is there something that I am missing here?

    ReplyDelete