logo



Making wonderful graphs

If your work is anything like mine, every once in a while you have to include a graph into a document. Where I work, everybody uses Microsoft Office with rather disastrous results. It generally goes like this:

  • You create a graph in Excel
  • You copy it and paste in into Word
  • Your graph becomes illegible and your Word document swells up to fill many megabytes. It becomes unmanageable and Windows crashes
It is much better to make wonderful graphs. All you need is two free programs called Gnuplot and Ghostscript. Both have versions for Windows and other OS's. You can download them, but check your local system first: free programs are often already installed on local networks.

Now then. Feel free to use Excel to tweak your data until you have the numbers that you want to graph. You can even make an Excel graph to see what things will look like, sort of. Just don't do anything else with it. Copy the numbers you want to graph into a simple text file. You can use Notepad to make the file, for instance. Leave the numbers in columns.

textfile

Easy enough. Save the file, then run Gnuplot on those numbers. The manual for Gnuplot is here: I encourage you to have a look. That said, commands typically look like this:

set terminal postscript eps color "Helvetica" 14

The truth is out: we will be making a postscript file. But don't worry, it will show up in Word as well. 14 point Helvetica is the font of choice.

set out "mygraph.eps"

We will write to this file. Please call it something else. Now, here we go:

plot "data.txt" u 1:2 t 'A' w lines lw 5,\
'' u 1:3 t 'B' w lines lw 5

This plots column one against column two, and then column one against column three. The first series has legend 'A',the second 'B'. Lines are a little thicker than standard, the '5' after 'lw' tweaks that.

Well, you now have a "mygraph.eps". Notice how small it is: usually around 15 kB. This is where Ghostscript comes in. Open the file using GSView (it's the program with the ghost-icon). You should see your graph in all its glory. Now we make it useful in MS Word by adding an eps-preview like so:

menu

Adding a TIFF 6 Packbits preview makes the file around twice as large.

Save the file as "newtitle.eps" (use your own imagination for the new title; remember you have to add the '.eps' yourself) and throw away the old eps-file. Now include the new file in Word the usual way: insert; picture; from file.

But what's the use of all this?

  • Your Word file will not become incredibly large, as it would with copy-paste action from Excel
  • The postscript information is still there. When you print to a PostScript printer or convert to Adobe pdf, the graphs look good on every scale. This beats saving graphs as JPG's, for instance.
  • Using Gnuplot, graphs are infinitely tweakable.

But I don't like the colors!

No problemo, mister. Just drag your newly minted .eps file into a text editor (before adding the TIFF preview). You should easily find these lines inside:

/LT0 { PL [] 1 0 0 DL } def
/LT1 { PL [4 dl 2 dl] 0 1 0 DL } def

These define colors for lines 1 and 2. The three numbers here stand for Red, Green and Blue, and can take values between 1 (on) and 0 (off): the first line tells you that line #1 is all Red, and no Green or Blue. Change these numbers, save the file and check the result in GSview.