7.8 Automating creation of graphs with gnuplot

gnuplot is good for automating generation of graphs for numerical data and/or mathematical functions. For "interactive" generation of graphs, I prefer any spreadsheet. As old-fashioned as gnuplot may look, it can be quite handy if you want to periodically re-generate a graph or visualize (for inspection) massive amounts of data from a graph "template". gnuplot is flexible (many options available, including 3d plots) but one needs to take your time to learn it. Setting up a complex graph can take me 2 hours (but it's OK, if the graph is to be re-used many times over). The best help is to start gnuplot, and on the "gnuplot>" prompt, type "help". gnuplot is available for Linux and MS Windows.

My data sets are stored in text (ASCII, *.dat) files. My "graph templates" are stored in gnuplot "command" files (*.gnu). The output goes to a graphics file (*.png) which can be printed or imported to any word processor.

To generate a graph from an example gnuplot command file "make_graphs.gnu", I can do:

gnuplot make_graphs.gnu

To display the graph, I would do (in X terminal):

display my_graph.png

My example "make_graphs.gnu" that generates an x-y graph follows.

# Comment are introduced with the hash (#)

# Stamp the graph with the current date and time

set timestamp "%Y-%m-%dT%T%z"

# This sets the graph resolution (the default is 100)

set samples 600

# Save the plot to a *.png file (make it colour)

set output "my_plot.png"

set terminal png color

#interesting terminals: png, x11, postscript, postscript eps, hpgl

set title "My Graph" # Graph title

set xlabel "Distance [m]" # title of x1 axis (bottom)

set x2label "Distance [feet]" # title of x2 axis (top)

set ylabel "sin meters" # title of y1 axis (left)

set y2label "log feet" # title y2 axis (right)

set xtics # control major tic marks on the axis

set x2tics; set ytics; set y2tics #commands can be separated with semicolons

set mytics # control minor tics on the axis, here I add them to the y axis

set xrange [0:15] # Range for display on the x1 axis

set x2range [0:15.0/0.305] #Expressions are ok. This one converts meters to feet.

set yrange [*:*] # The "*" sets the range to auto

set y2range [*:*] # Range for the y2 axis

set nologscale # or "set logscale x1x2y1y2" #Control logscale, linear scale is the default

set nogrid # or "set grid" #Control gridlines, no grid is the default

set key outside # or "set nokey" #Control legend and its positions: "top", "bottom", "left"

# The following line creates the plot with 4 graph series

plot sin(x) axes x1y1, log(x) axes x2y2, "data.dat" using 1:2, \

"data.dat" using 1:3

# Long lines can be split with \

# The third series uses columns 1 and 2 from the file

# The fourth plots the 3 column agains the 1st column from the data file.

8 Learning with Linux

Intro: This page contains commands (tools) which are not essential for newbies but can be fun and/or productive if you take the time to learn them. Above all, they can be very educational--Linux is a great learning platform. There are thousands of free programs available on Linux, but some of them may be missing on your system, depending on your distribution. You can always find the homepage with goggle, and then download and install them.

The entries below do not even pretend to be a full documentation for the command. For each command (tool) described, we try to give an example to illustrate the field of usability of the command. We try to make this example ready to run (self contained) and as useful as possible (we used them exactly for the tasks described). We hope this gets you started. After that, using man command, mozilling http://www.google.com may help to meet your specific requirement.

Suggest a Site