Dr. Dre ([info]andrel) wrote,

Gnuplot figure tutorial

I spent some time this evening screwing around with gnuplot, and discovered some neat stuff which I include here mostly for my own future reference, but also because one or two readers might care. My original intention was to email [info]sebbofeed some ASCII graphics ("set terminal dumb"), but in the end I learned how to output nifty LaTeX code. Besides, Thomas will explain it to you Thursday at dinner.

Here is the ljfig.gnuplot file:

set output "ljfig.eps"
set terminal epslatex '"default" 12'
set xlabel "number of subscribers"
set ylabel "cost to each subscriber"
set xrange [1:25]
set grid
plot 1/(log(x)/log(5) + 1) t "\\key    "

You compile it with something like "gnuplot ljfig.gnuplot && epstopdf ljfig.eps". And here is the lj.tex file:

\documentclass[12pt]{article}
\usepackage{graphicx,times}
\begin{document}
\begin{figure}
    \newcommand{\key}{$\left(\frac{\log(x)}{\log(5)} + 1 \right)^{-1}$}
    \input{ljfig}
    \caption{LiveJournal syndication costs}
\end{figure}
\end{document}

And here is the resulting figure (after running through pdflatex, and then converting back to PNG):

Notice that this is much closer to publication quality than anything Matlab ever produces. The text is all legible, not too small. The figure uses the same font as the caption below and the rest of the PDF file. The y-axis label is properly rotated and the function key is correctly typeset as a formula. The only flaw is the imperfect grid line behind the function key.

If you use Debian the gnuplot manual is separately packaged as gnuplot-doc.


  • Post a new comment

    Error

    Your reply will be screened

    Your IP address will be recorded 

  • 6 comments

[info]hissilliness

March 18 2004, 13:38:22 UTC 8 years ago

Wow, cool! I actually boned up on logorithms last night, so I have some idea what the formula says now. It seems unnecessarily complicated, though--couldn't they have gotten a similar curve from one to zero much more simply?

[info]crschmidt

March 18 2004, 16:00:10 UTC 8 years ago

Perhaps, but since computation of a logorithm is hardly complex math these days, it's not exactly a major CPU strain.

sub syn_cost
{
my $watchers = shift;
return 1 if $watchers < 1;
return 1/(log($watchers)/log(5)+1);
}


Not that bad. *shrug*

[info]andrel

March 19 2004, 05:34:53 UTC 8 years ago

A good optimizer would recognize that log(5) is a constant, though I don't think perl can do that.

I'm mildly curious, how did you stumble upon my journal entry?

[info]hissilliness

March 19 2004, 16:47:15 UTC 8 years ago

Probably my fault.

[info]andrel

March 19 2004, 19:08:10 UTC 8 years ago

For the record

My primary motivation in writing this up was to show how to make Gnuplot and PDFLaTeX interact, not explain anything about LJ. Producing good figures is hard, and I wanted to keep notes on one way of doing it.

[info]andrel

March 18 2004, 16:53:01 UTC 8 years ago

Well I only showed the early part of the graph. If I'd plotted it from 1 to 10,000 you'd notice this function has a very long tail. Although it falls off quickly between 1 and 10, it then falls off much slower. This function hits the value 1/(n+1) for 5^n readers. So for 1 reader it is 1. For 5 readers it is 1/2. For 25 readers it is 1/3. But you need to get to 1,953,125 readers for a cost of 1/10 point.
Create an Account
Forgot your login or password?
Facebook Twitter More login options
English • Español • Deutsch • Русский…