Gnuplot

Data Files

A typical Gnuplot data file consists of lines of text, where each line has two numbers, representing an x-value and a y-value. Here is a Gnuplot data file called “bars.dat”, followed by an explanation of its contents:

# number of days in each month of 2010
1 31
2 28
3 31
4 30
5 31
6 30
7 31
8 31
9 30
10 31
11 30
12 31

Explanation:

The first line, the one starting with #, is a comment.

Gnuplot will plot bars of size “y-value” at the corresponding “x-value”. For example, gnuplot will draw bars of size 31 at x-value 1, 3, 5, etc.

Plot Files

To plot the “bars.dat” data file, you use a file that contains Gnuplot commands. Here is an example file “bars.plot” that takes “bars.dat” as input and produces an output file “bars.png”. The graphic has an xrange of 0 to 13 so that all 12 months will appear and a yrange of 0 to 32. The “plot” command says to use “bars.dat” as the input file and plot the first column (1) as the x-value and the second column (2) as the y-value. The actual image produced appears after the listing of bars.plot:

set terminal png large
# Modify to change the output file
set output "bars.png"
set data style boxes 
set boxwidth 0.4 
set xtics nomirror
set border 11

# Modify this code to set the x-range  
set xrange [0:13]

# Modify this line to set the y-range
set yrange [0:32]

set xlabel "Months" 
set ylabel "Days in Month"
set xtics ("Jan" 1, "Feb" 2, "Mar" 3, "Apr" 4, "May" 5, "June" 6,\
"July" 7, "Aug" 8, "Sep" 9, "Oct" 10, "Nov" 11, "Dec" 12) 
set key below

plot 'bars.dat' using 1:2 fs solid title "Num Days"

Executing Gnuplot

To execute Gnuplot, in the terminal run gnuplot <plotfile>

For the above example, you would execute

gnuplot bars.plot

There should be no output (gnuplot is a bad parent), but you should now see the output file in the directory if you run ls.

To create your own graph file, you will probably need to modify

  • the output file's name
  • the input file's name
  • the x-axis
  • y-axis range and labels
  • the xtics (x-axis labels).
labs/gnuplot.txt · Last modified: 2011/05/01 17:49 by admin
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0