Tip 19: GNUplot

From Vlsiwiki
Jump to: navigation, search

GNUplot is a free plotting utility. It is one of the most flexible ones available and includes options for 3D, heat plots, animation, splines, polar, etc. It also has basic statistics capabilities, but it is often best to do that in another language such as Python or Perl. This tutorial will only cover the very basics of GNUplot. For more complete examples, see the demo page: http://gnuplot.sourceforge.net/demo/ You should also make sure to look at the demos for the appropriate version. The recent versions from the last few years have had some great advances in visualization.

GNUplot is nice because it can save in very flexible formats and it is portable among Mac, PC, Linux. It also doesn't charge a license fee so you can always recreate your plots without a Matlab or Vizio or M$ license. It is most often useful to save directly as a .eps or .pdf file. These are "vector" formats which means that they can scale to any size and do not lose resolution. As opposed to a .gif or .bmp which is a bit-map image and loses resolution when scaled.

You can run gnuplot either in interactive mode or batch mode. Just typing gnuplot at the command line will start up interactive mode with a "gnuplot>" prompt. Batch mode is run by supplying a script to gnuplot with a command like "gnuplot myplot.gpl". The .gpl file contains the same commands as interactive mode. If you do not specify the output, gnuplot will display the result on your current screen. If you want to save the output to a file, you must specify the format and the file name like this:

set terminal eps
set output "myfile.eps"

If you just type "set terminal" it will list the available output formats. (If pdf is not available, you can save as eps and then run eps2pdf.)

You can plot basic data in one of two ways. The first is with inline data such as

plot '-'
0 1
1 2
3 4
5 6
e

The hyphen tells the plot command contains the data. If you replace that with a file, it will take data from the file:

plot 'myfile.dat'

where myfile.dat contains

0 1
1 2
3 4
5 6

Note that the "e" is short for "end" and terminates a data set. You can supply more than one data set to a plot command and more than one plot command per batch file. For example, the following will plot two sets of data on one pair of axes:

plot '-' ti 'Data Set 1','-' ti 'Data Set 2'
0 1
1 2
3 4
5 6
e
5 1
3 2
2 4
1 6
e

It will also give each of them the title (via the "ti" argument).

To set the axis labels you should use commands like:

set xlabel "Houses"
set ylabel "Cars"

Another useful thing is to change the formatting of the plot command. You can specify the type of lines and points like this:

plot '-' with linespoints

or in a shorter way

plot '-' w lp

You can select specific styles and widths like this

plot '-' with lp lt 3 lw 3 ps 2

where lt means linetype, lw means linewidth, and ps is pointsize. To see all of the options available in a given terminal, you can generate the demo of the styles with the "test" command.

To get help on any GNUplot command, you can type "help <command>" in interactive mode. Specify as much of the command as possible. For example:

help set

will give help on the set command. Whereas,

help set style

will give help on setting styles for the entire plot. Whereas, even further,

help set style boxes

will give help specifically about box styles.

Other things you can do with gnuplot:

* plot 3D mesh structures and surface plots
* animations
* multiple x- and y-axes
* multiple embed plots
etc.

Take a look at the demos to find more. More will be added here as needed.