Cheat Logo

Cheat Sheet

Hints for Graphing

  1. gnuplot palettes
  2. Labeling Images
  3. Learning gnuplot
  4. Making graphs look good
  5. Making graphs readable
  6. Reading data from a file
  7. Three-dimensional data

gnuplot palettes

Last updated: 2010-09-27
Level: Beginner
OS:
  • Windows
  • OS X
  • Linux
  • BSD
  • Solaris
Tags:
Programs:

It can be helpful to use a defined color palette for three dimensional map plots. For example, if your data ranges from -2 to 2, you can make the positive and negative values, as well as zero, obvious by using a scale like this:

set palette defined (-2 "blue", 0 "white", 2 "green")

Labeling Images

Last updated: 2010-09-27
Level: Intermediate
OS:
  • OS X
  • Linux
  • BSD
  • Solaris
Programs:

Add label to southeast corner:

convert -font Verdana-Regular -pointsize 9 -background '#00000080' \
 -fill lightblue label:'schutt.org' miff:- | composite -gravity southeast \
 -geometry +0+0 - in.png  out.png

Add text to bottom:

montage -geometry +0+0 -font Verdana-Regular -pointsize 9 \
 -background 'gray(20%)' -fill skyblue \
 -label "Some text goes here" in.png out.png

Learning gnuplot

Last updated: 2010-09-27
Level: Intermediate
OS:
  • OS X
  • Linux
  • BSD
  • Solaris
Tags:
Programs:

The best introductions to gnuplot can be found at:

Making graphs look good

Last updated: 2010-09-27
Level: Intermediate
OS:
  • OS X
  • Linux
  • BSD
  • Solaris
Tags:
Programs:

If you intend to display a graph on a webpage, the best format to use is PNG. Unfortunately, the gnuplot PNG terminal doesn't always produce the best looking graphs. You can often get better results by plotting with dimensions double the final size you want, then resampling the image. For example, if you want to make a graph 300x300 pixels with 9 point labels, plot at 600x600 with the font size set to 18. Then resize the image. An easy way to do this is with ImageMagick. My plot commands will look something like this:

set terminal png enhanced \
   font "/Library/Fonts/Verdana.ttf" 18  \
   size 2*300, 2*300
set output "plot.png"
set title "Title Text" -2,0 font "/Library/Fonts/Verdana.ttf,22"
set ylabel "Y label" 1.2,0  font "/Library/Fonts/Verdana.ttf,20"
set xlabel "X label" font "/Library/Fonts/Verdana.ttf,20"
plot "data.csv" using 1:2 with lines lc rgb "skyblue" lw 4 notitle;

Then I'll resize the plot from the command line (bash) using:

mogrify -geometry 50% myplot.png

or

convert -geometry 50% myplot.png myplot-small.png

The smoothing done when downsampling lowers apparent precision in addition to making the graph look better. This isn't really a problem, because if you need the precision, you will probably be linking the small plot to a larger version or the original data.

An alternative is to plot using the PostScript terminal, then convert to PNG. This has the advantage that you already have a PS file to link to.

Making graphs readable

Last updated: 2010-09-27
Level: Intermediate
OS:
  • OS X
  • Linux
  • BSD
  • Solaris
Tags:
Programs:

Verdana is a good typeface for making small labels readable. If you have lots of text, Lucida Grande is a good choice.

Reading data from a file

Last updated: 2010-11-11
Level: Intermediate
OS:
  • OS X
  • Linux
  • BSD
  • Solaris
Tags:
Programs:

I usually use gnuplot to plot data from a file, typically stored as a csv or tsv spreadsheet. gnuplot needs to be told which format to use:

  • set datafile separator "," for comma separated
  • set datafile separator "\t" for tab separated

For each plot command, you must tell gnuplot which data file to use. For example, to plot columns 5 and 6 vs column 1 in 'foo.csv' and column 2 vs 1 in 'bar.csv':

set datafile separator ","
plot 'foo.csv' using 1:5,\
     'foo.csv' using 1:6,\
     'bar.csv' using 1:2

If you are plotting multiple columns of data and some odd values are showing up, it may be due to missing data. In some cases you need to manually tell gnuplot what to do with missing data. For example, if missing points are simply left blank, use the command:

set datafile missing ""

Three-dimensional data

Last updated: 2010-09-27
Level: Intermediate
OS:
  • OS X
  • Linux
  • BSD
  • Solaris
Tags:
Programs:

When graphing 3d data, it is often clearer to use a color map than to plot a surface in three dimensions. Use a clear color scale with an obvious mapping. This will make it easier to read. See 3-Dim Plot with a Color-Map (pm3d) for examples.

To plot something like this:

you can use these commands:

set terminal png size 600,600 crop # output a 600x600 pixel png image
set pm3d map # use 3d color map plot
set xrange [1:321] # manually set range of x-axis
set yrange [1:321]
set size square # make the plot square
unset key # don't show key
set format x '' # don't show axis label
set format y ''
set cbrange [0:25] # the range of the z data
set output 'out.png' # the output file name
set title 'the title' # graph title
splot 'data.out' using 1:2:3 # plot the data file using the first three columns
©2015 Schutt Design (Luke & Noel Schutt).
Contact us using one of our first names at schuttdesign dot net.