The Gnuplot Graphing program

Table of Contents

last revision
27 October 2011, 6:33pm
book quality
useful

Gnuplot is an amazingly capable program with the usual plethora of switches, options, codes, and hidden and secret corners only explored by university thesis writers, who occasionally publish a desultory web-page describing some serendipitous discovery.

Gnuplot can make mathematical graphs and curves as well as bar-charts (histograms) along with lots of other things. In this book, the convention of writing '$>>' may be used to indicate commands which are entered from within the gnuplot interpreter.

www: http://www.gnuplot.info/
The official home page of gnuplot
www: http://www.ibm.com/developerworks/library/l-gnuplot/
a good tutorial
www: http://sparky.rice.edu/gnuplot.html
an intro to gnuplot
www: http://t16web.lanl.gov/Kawano/gnuplot/plot1-e.html
lots of info
start the interactive gnuplot shell
 gnuplot

History ‹↑›

Originally developed by Colin Kelley and Thomas Williams in 1986

Getting Help ‹↑›

view a general help menu with submenu selections

 help

get help for the gnuplot 'set xrange' command

 echo help set xrange | gnuplot

display test graphs with example linewidths and pointtypes

 test

view a list of all keywords setting with the 'set' command

 set

view a list of all possible options after the 'with' keyword

 plot x with

Command Abbreviations ‹↑›

Any command can be abbreviated to any length as long as there is no ambiguity, that is, no other command which could be referenced by the same abbreviation (eg 'u' or 'usi' for using) . Other commands have and explicit abbreviation (eg 'lt' for 'linetype')

Simple Usage ‹↑›

Gnuplot can be used either 'interactively' by starting the gnuplot program with the command 'gnuplot' or else it can be used in batch mode executing commands to create graphs.

start the gnuplot shell and plot a polynomial graph

    gnuplot    
    # a welcome message is shown and a new 'gnuplot' prompt starts
    plot x**3 + x**2 + x + 1
    #the graph is shown in a separate window
    quit  
    # exit gnuplot, the graph window is also closed

plot a sin curve with x-axis (horizontal axis) -20 to 20

 set xrange [-20:20]
 plot sin(x)

plot a surface with colorful lines

 echo "splot x*x-y*y with line palette" | gnuplot -persist

plot a parabola using pipes

 echo 'plot x**2' | gnuplot -persist

plot sin, cos and tan curves all on the same graph

 echo "plot sin(x),cos(x),tan(x)" | gnuplot -persist

plot a surface from the bash shell using an 'x' and 'y' range

 echo 'splot [x=-4:4] [y=-4:4] sin(x)*cos(y)' | gnuplot -persist

plot some random disk usage data of the current folder, labels rotated

   du -s * | shuf | head -10 > temp.txt
   echo '
     set style data histogram
     set xtics rotate by -60
     plot "temp.txt" using 1:xticlabels(2)' | gnuplot -persist

The 'xticlabels' should refer to the field containing text (not numbers)

plot disk usage data from standard input

 (echo "set xtic rot;plot '-' u 1:xticl(2)"; du -s ~/*)|gnuplot -persist

a more verbose version of the same

  (echo "
      set xtics rotate by -45;
      plot '-' using 1:xticlabels(2)"; 
      du -s ~/*)|gnuplot -persist

create an image of a sin curve graph and display it

 echo "se t png; se o 'o.png'; p sin(x)" | gnuplot; feh o.png

the same as above but more verbose

 echo "set terminal png; set output 'o.png'; plot sin(x)" | gnuplot; feh o.png

plot the graph 'sin(x)' as ascii text

 echo "set terminal dumb; plot x**2" | gnuplot -persist
 echo "se te du; p sin(x)" | gnuplot -persist  the same

Command Line Options ‹↑›

Axis Value Range ‹↑›

plot a sin curve with x-axis -20 to 20, all commands on one line

 set xrange [-20:20]; plot sin(x)
 plot [-20:20] sin(x)      the same

plot a sin curve with x-axis -pi to pi ('pi' is a mathematical transcendental)

 set xrange [-pi:pi]; plot sin(x)
 plot [-pi:pi] sin(x)   almost the same

plot the function 'y=x' with a y-axis range of [-2:5]

 plot [][-2:5] x
 plot [  ] [-2:5] x         the same, spaces dont matter
 set yrange [-2:5]; plot x  the same, but persists for next graph

set the x value range to 0 to a multiple of pi

 set xrange [0:2*pi]

plot from the command line a 'cos(x)' graph with x-range -20 to 20

 echo 'plot [-20:20] cos(x)' | gnuplot -persist

create an image of 'sin(x)' with x-range -20 to 20

 echo "se t png;se o 'i.png'; p[-20:20] sin(x)" | gnuplot;feh i.png

the same as above

 echo "set t png;set o 'o.png';plot [-20:20] sin(x)" | gnuplot;feh o.png

Using The Gnuplot Shell ‹↑›

plot a curve, add a title and then replot it (which saves some typing)

 plot sin(x); set title 'hello'; replot

span a single command over several lines with a backslash '\'

 set \
 grid

Graph Titles ‹↑›

The title of the graph normally appears above the graph and should describe it. Each curve or data set should also have a legend to explain it.

plot a graph of the sin curve with a graph title 'a simple curve'

 echo 'set title "Two Curve"; p sin(x),tan(x)' | gnuplot -persist

set the title for just one plot

 echo "plot x*x title 'a parabola'" | gnuplot -persist
 plot x*x t 'a parabola'   exactly the same

create a multiline title for a half circle graph

 echo 'p [-2:2][0:2](1-x*x)**0.5 title "Half\nCircle"'|gnuplot -persist

plot a curve with the title 'x^2 + y^2'

 echo 'set title "x^2 + y^2"; splot x**2+y**2' | gnuplot -persist

Borders And Frames ‹↑›

The border is the box which surrounds the graph and on which the tick marks are drawn.

Erasing Borders ‹↑›

To erase some or all or the borders of a graph one must use a sort of binary arithmetic.

remove all borders from the graph (but the ticks remain)

 set border 0

display only the bottom and left borders, tic marks and labels

 echo "se bor 3; se tics nomirror; p sin(x)" | gnuplot -persist
 echo "se bor 3; se tics nomi; p sin(x)" | gnuplot -persist  the same

display only the bottom and left borders, tic marks and labels

 set border 3; set xtics nomirror; set ytics nomirror
 set border 3; set tics nomirror   the same, with modern gnuplot
 se bor 3; set tics nomirror       an abbreviated version

show the bottom, left, right borders, but not the top, nor the top tics

 echo "se bor 11; se xtics nomi; p x**2" | gnuplot -persist

show no graph border, no tic marks and no value labels for a parabola

 unset tics; set border 0; plot x**2
 set notics; set border 0; plot x**2  an older syntax

display only the bottom border for a parabola graph

 set border 1; plot x**2

example
using - 'u' 'usi', 'usin' are all valid abbreviations

the graph border 'keys'
1 - bottom
2 - left
4 - top
8 - right

Axis Labels ‹↑›

The axis label is the text which is printed alongside of the vertical (y) or horizontal (x) axis. This text generally explains the meaning of the value of that axis, such as 'temperature' or 'date' etc.

The formal syntax statement

    set xlabel {"<label>"} {offset <offset>} {font "<font>{,<size>}"}
           {{textcolor | tc} {lt <line_type> | default}} {{no}enhanced}
           {rotate by <degrees>}

show the settings which currently apply for the x-axis label

 show xlabel

suppress the label on the vertical axis (no label is shown)

 set ylabel

set the vertical axis label to be the text "degrees"

 set ylabel "degrees"
 set ylabel 'degrees'      the same, but maybe not for all versions

plot a graph of a parabola (x squared) setting the y-axis label

 echo 'set ylabel "the y range"; plot x**2' | gnuplot -persist
 echo 'set yl "the y range"; p x**2' | gnuplot -persist the same

plot a graph of free disk space setting the y-axis label

   (echo '
     set style histogram clustered gap 1; set boxwidth 1.5
     set style data histogram 
     set style fill solid
     plot [][0:15] '-' using 2:xticlabels(1)"; 
     # the xtic labels are almost vertical upwards
     set xtics rotate by 91 
     set ylabel "1K blocks used"
     plot "-" using 1:xticlabels(2)'
     df| grep [0-9]%| tr -s ' '|cut -d' ' -f3,6)| gnuplot -persist 

set the y range label succintly

 echo 'set yl "Y Value"; p [-20:20] sin(x)/x' | gnuplot -persist

set the y range label succintly

 echo 'set yl "Y Value"; p [-40:40] sin(x)/(4*x)' | gnuplot -persist

put a newline character in the y-axis label

 set ylabel "Degrees\ncelsius"
 set ylabel 'Degrees\ncelsius'   No! it doesnt work, no escapes

create & display an image of a graph with a multiline y-axis label

 echo 'set t png; set o "i.png"; set yl "A\nVal"; plot x**2' | gnuplot; feh i.png

plot a graph of sin(x) setting the text of the x-axis and y-axis labels

 set xlabel "Angle, \n in degrees"; set ylabel "sin"; plot sin(x)

plot a graph from the shell of sin(x) with x-axis and y-axis labels

 echo 'se xl "Angle\n(deg)"; se yl "sin"; p sin(x)/cos(x)' | gnuplot -persist

plot a graph from the shell of sin(x) with x-axis and y-axis labels

 echo 'set grid; se xl "Angle\n(deg)"; p sin(x)/cos(x)'|gnuplot -persist

Axis Label Font And Size ‹↑›

get help for setting the xlabels

 help set xl

show some help for setting the font for the label

 echo 'help set font' | gnuplot

show the current font path

 echo 'show fontpath' | gnuplot

set the font for the vertical axis label to Courier, size 14 points

 set ylabel font "courier,14"

create a y-axis label 'range' in courier font, size 14 points

 echo 'set yl "Range" font "courier,16"; p sin(2*x)' | gnuplot -persist

create a y-axis label 'range' in size 18 points in the default font

 echo 'set yl "range" font ",18"; plot x**2' | gnuplot -persist

make a pink 18 point y-axis label 'val' in the default font

 echo 'set yl "val" font ",18" tc lt 4; plot x**2' | gnuplot -persist

make a pink 18 pnt y label 'val' rotated by 30 degrees anti-clockwise

 echo 'se yl "val" font ",18" tc lt 4 rot by 30;p x**2'|gnuplot -persist
 echo 'set yl "val" font ",18" tc lt 4 rotate by 30;p x**2' | gnuplot -persist

set the font for the horizontal axis label to Courier,20

 set xl "values" font "courier, 20"; plot x**2

set the horizontal axis label to the colour specified by 'linetype 4'

 set xlabel textcolor lt 4   but what colour is lt 4? you ask
 set xlabel tc lt 4          the same
 se xl tc lt 4               the same again

Rotating The Axis Label ‹↑›

This isnt working for me

rotate the label ?

 echo 'set xlabel "Date" rotate by -45; plot x**3' | gnuplot -persist

make a pink 18point, label 'val' rotated by 30 degrees anti-clockwise

 echo 'se yl "val" font ",18" tc lt 4 rot by 30;p x**2' | gnuplot -persist
 echo 'set yl "val" font ",18" tc lt 4 rotate by 30;p x**2' | gnuplot -persist

Moving The Axis Label ‹↑›

move the x axis label 1 character width to the left

 set xlabel offset -1,0

Customizing The Axis Label ‹↑›

make a yellow 15 point horizontal label 'hello' in helvetica font

 set xlabel "hello" font "helvetica" textcolor lt 6
 set xlabel "hello" font "helvetica" tc lt 6
 echo 'se xl "Hello" font "helvetica,15" tc lt 6; p x**2' | gnuplot -persist

Graph Size And Scale ‹↑›

make the graph an exact square (regardless of value ranges

 set size square   only available in modern versions of gnuplot

scale the graph so that the y (vertical) axis is twice as long as the x-axis

 set size ratio 2

scale the graph so that the x (horizontal) axis is twice as long as the y-axis

 set size ratio 0.5

scale y-axis by 2, retain x-axis size

 set size ratio square 1,2

Grids ‹↑›

plot a sin curve over a grid of dotted lines (grid lines at each value 'tic') ::gnuplot>> set grid; plot sin(x)

remove a value grid from a plotted graph

 unset grid; replot;
 set nogrid; replot;    older versions of gnuplot

The Value Ranges ‹↑›

plot only data from 0.5 to 10 on the x-axis and 30 to 48 on the y-axis

 set xrange [0.5:10]; set yrange [30:48]; plot 'data.txt'
 plot 'data.txt' [0.5:10][30:48]   this is the same

plot data using using an x-range of 0 to the highest x value

 set xrange [0:]; plot 'data.txt' the horizontal axis starts at 0

reset the value ranges previously set with 'set xrange' or 'set yrange'

 reset    the value ranges revert to the defaults

Yrange ‹↑›

On a two dimension graph (or chart) the yrange in the vertical range. By default when plotting "bar-charts" (also called "histograms") gnuplot uses the maximum value as the upper y-range, which in my opinion is unpleasant.

plot a graph with the default xrange, and y-range -20 to 20

 echo "plot [][-20:20] sin(x),cos(x),tan(x)" | gnuplot -persist

plot a graph setting the yrange explicitly to -20: 20

 echo "set yrange [-20:20]; plot sin(x),cos(x),tan(x)"|gnuplot -persist

yrange can be abreviated to 'yr'

Tics ‹↑›

'Tics' are those small little lines which 'mark' or are perpendicular to the axis (either horizontal or vertical). These tics are supposed to indicated value points or ranges. I think it should be spelt 'tick' but in gnuplot its spelt 'tic'.

view help for all the options for changing the x-axis value tics

 help set xtics

show information about all options currently set for the ytics

 show ytics

show the vertical (y-axis) tics outside of the axis line

 set ytics out   this affects the 'mirrored' opposite tics as well

plot a curve with no 'tics' or value labels on the horizontal axis

 unset xtics; plot sin(x)

display vertical tics at intervals of 2 (by value)

 set ytics 2

display horizontal tics only starting at 50 with interval 100

 set xtics 50,100    values outside this range are still plotted

set x (horizontal) tics at the values 1, 2, 4, ... 1024

 set xtics (1,2,4,8,16,32,64,128,256,512,1024)

set y (vertical) tics at the values 1, 11, and 21

 set ytics (1,11,21)

Minor Tics ‹↑›

Minor tics are the even smaller little markes which occur on the horizontal and vertical axis

display one minor tic halfway between each major tic

 set mxtics 2

make the minor and major tics the same length

 set ticscale 1 1

Tic Labels ‹↑›

plot a paraboloa with the x-axis labels rotated clockwise by -45 degrees

 set xtics rotate by -45; plot x**2

rotate the tic labels by 90 degrees

 set xtics rotate

plot a sin curve with only 3 x-axis value tics, on a grid

 set xtics ("0" 0, "90" pi/2, "-90" -pi/2); set grid; plot sin(x)
the grid only has 3 vertical lines, since there are only 3 x-axis tics

set the value tics for the x-axis (format: "label" value [level])

 set xtics ("0" 0, "90" pi/2, "-90" -pi/2, "" pi/4 1, "" -pi/4 1, "" 3*pi/4 1, "" -3*pi/4 1)

explicit 'tic' examples

    set xtics ("low" 0, "medium" 50, "high" 100)
    set ytics ("bottom" 0, "" 10 1, "top" 20)

Text Tick Labels ‹↑›

The tic label is the text which sits just next to the little tick mark on the vertical or horizontal axis. These labels can be customized in gnuplot in many ways.

explicitly set three tic text labels at the values 0, 50 and 100)

 set xtics ("low" 0, "medium" 50, "high" 100)

dont put tics on the oposite side to the main y-axis (vertical axis) $>> set ytics nomirror there are no tics but the oposite box-line stays

display 3 tics on the vertical axis with no tic labels

 du -s ~/*>j;echo 'set xtics rot by -45; set ytics ("" 0, "" 10, "" 20); p "j" u 1:xticl(2)' | gnuplot -persist

display 3 tics on the vertical axis with no tick labels (no text at the tick)

 du -s ~/*>j;echo 'set ytics ("" 0, "" 10, "" 20);p 'j' u yticl(2):1' | gnuplot -persist

add a tic and label 'Pi' on the x axis without affecting the default tics

 set xtics add ("Pi" 3.14159)

make tics on the y-axis 0,.5,1,1.5...10 and added one label 'Pi' at 3.141

 set ytics 0,.5,10; set ytics add ("Pi" 3.141)

Tic Intervals ‹↑›

display the vertical tics at value intervals of 5

 set ytics 5   this places tics at ... -10, -5, 0, 5, 10 etc

display the x-tics starting at value 0, ending at 10 with an interval 0.5

 set xtics 0,0.5,10  this show values 0, 0.5, 1, 1.5 ... 9.5, 10

The Data Plotting Style ‹↑›

Strangely theres no simple 'pie' chart in gnuplot

border examples
set border 3 - (1+2) display bottom and left borders
set border 6 - (2+4) display left and top borders
set border 6 - (1+2+4) display bottom left and top borders
set border 11 - (1+2+8) display the bottom left, and right borders

set the style of data plotting to 'histogram'

 set style data histogram

histograms of disk usage of the current folder, labels rotated

du -s * > j; echo ' set style data hist; set xtics rot by -60; plot "j" using 1:xticlabels(2)' | gnuplot -persist ,,,

Histograms are not normally contiguous

histograms of 1st 20 results of disk usage of the current folder, with the labels rotated by 60 degrees

 du -s * > j; echo 'set style data boxes; set xtics rot by -60; plot [0:20] "j" using 1:xticlabels(2)' | gnuplot -persist

Getting Help ‹↑›

view a list of the available plotting styles

 plot x with

view help for different curve plotting styles

 echo help with | gnuplot       lots of examples
 echo help plot with | gnuplot  the same
 help plotting styles           gnuplot version >= 4.0

show the current setting for what style of data plotting

 show style data

see a list of valid data plotting styles

 set style data

Plotting With Points ‹↑›

plot a parabola with points

 plot x**2 w p
 plot x**2 with points              the same
 set style data points; plot x**2   the same, but persistent
 set style data p; plot x**2        the same, again

plot the line 'y=x' with points (crosses) which are 4 times the normal size

 plot x w points pointsize 4   the default point seems to be a cross
 plot x w points ps 4          the same
 plot x w p ps 4               the same, again

plot a parabola with a line with 'x'es at value points

  plot x**2 with linespoints
  plot x**2 w linespoints                the same
  plot x**2 w lp                         the same again, nice and terse
  set style data linespoints; plot x**2  the same, persistent

plot a parabola with a line with little boxes at value points

 plot x**2 with linespoints pointtype 5
 plot x**2 w lp pt 5         the same, not so verbose

plot a parabola with 'boxes' or contiguous vertical bars (a histogram)

 plot x**2 w boxes     this looks like a mathematical bar-chart
 plot x**2 with boxes  the same

plot a parabola with purple bars

 plot x**2 with boxes lt 4
 plot x**2 with boxes linetype 4   the same

plot a sin and a cos curve on one field with different curve types

echo "plot sin(x) w linespoints pointtype 5, cos(x) w boxes" | \ gnuplot -persist ,,,

data plotting styles
points - each value a point or a cross or a box etc
histogram - the usual bar graph
lines - a straight line from each data value to the next
linespoints - a line with points to indicate values
steps - looks like a city skyline
boxes - these look like a histogram and are normally contiguous
errorbars - a vertical line showing an error range for each datapoint
impulses - lines which go from the axis to the value point
and others ...

linespoints styles
linespoints pointtype 5 - like a beaded necklace boxes on a line

Impulses ‹↑›

plot disk use data with impulses

 echo "plot '-' using 1:xticl(2) with impulses; $(du -s ~/*)" | gnuplot -persist

The Graph Legend ‹↑›

The 'legend' or 'key' of the plot describes what each curve (or dataset) actually means and by default is in the top right corner. In gnuplot the legend is called the 'key'

 echo help set key | gnuplot

put a box around the graph legend and place it in the top left corner

 echo "set key top left; set key box; plot x**0.5;" | gnuplot -persist

places the key in the bottom left corner, left-justified text with it a title, and draws a box around it in linetype 3:

    echo  "
     set key left bottom Left title 'Legend' box 3
     splot x*x-y*y" | gnuplot -persist

Side By Side Graphs ‹↑›

side by side

# multiplot mode # This sets up bounding boxes and may be required on some terminals set size 1,1 set origin 0,0

# Done interactively, this takes gnuplot into multiplot mode # and brings up a new prompt ("multiplot >" instead of "gnuplot >") set multiplot

# plot the first graph so that it takes a quarter of the screen set size 0.5,0.5 set origin 0,0.5 plot sin(x)

# plot the second graph so that it takes a quarter of the screen set size 0.5,0.5 set origin 0,0 plot 1/sin(x) unset multiplot reset ,,,

Drawing Arrows ‹↑›

draw some arrows

 set arrow from 1,2 to 4,8.4 nohead lt -1 lw 1.2

Plotting Data ‹↑›

The process of plotting data with gnuplot involves taking a text file which has 'fields' separated by a 'separator' character or characters (usually a space or tab characters) and turning that data into a graph or chart. Often this chart would be a 'histogram' (that is a 'bar-chart), but other forms are possible.

www: http://www.linuxquestions.org/questions/linux-software-2/gnuplot-how-to-label-x-axis-with-strings-from-data-file-462635/
How to use a field to supply the x-tic labels (horizontal axis value labels)
The word 'field' and 'column' are used interchangably.

Data Sources ‹↑›

The data which you which to visualise in a graph can come from a variety of sources. It is also possible to take it from the 'standard in' of the system, using the usual piping mechanisms.

plot disk use data with impulses

echo " set xtics rotate by -45 plot '-' using 1:xticl(2) with impulses; $(du -s ~/*|head -10)" | gnuplot -persist ,,,

prepare some data about last logon times using tr, cut etc

 last reboot|grep reboot|tr -s ' '|cut -d' ' -f5-6|tr ' ' .|uniq

Using 'tr' and 'cut' in tandem like this allows us to extract fields from the text data (since cut can only handle field delimiters of one character). It would be possible to use awk instead but I like the simplicity of this.

The Gotchas ‹↑›

file names must be enclosed in quote characters (unless its the shell)

 plot data.txt     No! doesnt work
 plot 'data.txt'   correct

the datafile should only contain numbers (or use 'xticlabels' etc)

datafile: italy 2 spain 5

 plot 'data.txt'   this produces an error because of the text 'italy' etc
,,,

if the data file contains text (not numbers), use 'using' to avoid that field

 plot 'data.txt' using 2:3  the first field may contain text

Basic Data Plotting ‹↑›

list the contents of the text data file 'data.txt'

 !less data.txt

plot data using column 1 for the x-axis and column 2 for the y-axis

 plot 'data.txt' using 1:2  data is plotted with little crosses '+'

plot data using field 3 for the x-axis and field 2 for the y-axis

 plot 'data.txt' using 3:2

plot data which is entered at the terminal, (useful for experimenting)

plot '-' 1 10 2 20 3 5 ,,,

plot disk usage without making a temporary file

(echo " set xtics rot; plot '-' using 1:xticlabels(2)"; du -s ~/*) | gnuplot -persist ,,,

the same as above but more terse

 (echo "set xtic rot;plot '-' u 1:xticl(2)"; du -s ~/*)|gnuplot -persist

plot disk usage with lines using only the first 10 results

(echo " set xtics rot by -60; plot [0:10] '-' using 1:xticlabels(2) with lines"; du -s ~/*) | gnuplot -persist ,,,

plot disk usage with lines using only the first 10 results

(echo " set terminal dumb 70 25 set xtics rot by -60; plot [0:10] '-' using 1:xticlabels(2)"; du -s ~/*) | gnuplot ,,,

do something

 (echo "set xtics rotate by -45; plot using 1:xticlabels(2) '-' "; du -s ~/* ; ) | gnuplot -persist

Plotting Data With Value Ranges ‹↑›

The default data range is from the minimum value to the maximum + 1 on both the x and y axes.

plot the data using a horizontal (x) range of 0 to 20 and the default y-range

 plot [0:20] 'data.txt'
 set xrange [0:20]; plot 'data.txt' the same, but persists

plot field 2 vs field 3 using an x range of -10 to 10 and a y-range of -5 to 6

 plot [-10:10][-5:6] 'data.txt' using 2:3

plot column 2 vs 3 using the y value range of -5 to 5 and the default x range

 plot [][-5:5] 'data.txt' using 2:3
 set yrange [-5:5]; plot 'data.txt' using 2:3  the same, but persists

start the x and y value range at -10 to their respective defaults

 plot [-10:][-10:] 'data.txt'

plot the data file 'data.txt' with the x (horizontal) range starting at 0

 use xrange [0:]; plot 'data.txt'  file can only contain numbers
 plot [0:] 'data.txt'              the same

Plotting Text Value Data ‹↑›

plot direct from standard in

 (echo "set xtics rotate by -45;plot '-' using 1:xticlabels(2)"; du -s ~/*)|gnuplot -persist

plot some data as histograms

  (echo "
    set xtics rotate by -45
    set style histogram clustered gap 1; set boxwidth 1.5
    set style data histogram 
    plot [][0:15] '-' using 2:xticlabels(1)"; 
    echo "
      france 10
      italy 14
      spain 2
      england 5")|gnuplot -persist

plot 'list.txt' using field 2 for the values and taking x-axis labels from the first column of the datafile

 plot "list.txt" using 2:xticlabels(1)   gnuplot version >= 4.1
 plot "list.txt" u 2:xticlabels(1)       the same

plot some text versus number data from the bash command line

 echo 'plot "list.txt" using 2:xticlabels(1)' | gnuplot -persist

plot label vs value data using boxes instead of little crosses

 plot 'data.txt' using 2:xticlabels(1) with boxes almost like a barchart
 plot 'data.txt' using 2:xticlabels(1) w boxes    the same
 plot 'data.txt' u 2:xticlabels(1) w b            the same, but better

plot text vs value data using lines (a crooked line)

 plot 'data.txt' u 2:xticlabels(1) w l
 plot 'data.txt' using 2:xticlabels(1) with lines the same

By default the labels are printed horizontally

plot home disk use data with labels from the 2nd column, rotated 45 degrees clockwise

 du -s ~/* > j.txt; echo 'set xtics rotate by -45; plot "j.txt" using 1:xticlabels(2)' | gnuplot -persist

plot the first 10 results of diskusage

 echo "set xtic rot by -45; plot [0:10] '-' using 1:xticl(2); $(du -s ~/*)"|gnuplot -persist

a more concise and cryptic version of the above

 (echo 'set xtic rot by -45; p "j" u 1:xticl(2)';du -s ~/*) | gnuplot -persist
use lines starting at the axis going to the value to graph the data
 plot "data.txt" u 2:xticlabels(1) w imp

define a new thick linestyle and use for a fake barchart

#set linestyle 1 lt 1 lw 50; #set linestyle 1 linetype 1 linewidth 50 the same # modern versions echo " set style line 1 linetype 1 linewidth 50 plot '-' u 2:xticlabels(1) with impulses linestyle 1; $(du -s *)" | gnuplot -persist ,,,

 plot 'd.txt' u 2:xticlabels(1) w imp ls 1     the same_

Error Bars ‹↑›

plot data which contains an 'error margin' in the 3rd column

  plot "test.dat" using 1:2:3 with yerrorbars
  example data:
  1.0   1.2   0.2
  2.0   1.8   0.3
  3.0   1.6   0.2

Bar Charts ‹↑›

www: http://t16web.lanl.gov/Kawano/gnuplot/plot5-e.html
examples of drawing barcharts with 'impulses'
The simplest way to plot a bar-chart or histogram is to use the 'histograms' plotting style (after 'with') The 'histograms' is by default separated and the 'boxes' are by default contiguous.

Bar Charts With Histograms ‹↑›

the linespoints pointtype values
4 - empty boxes
5 - filled boxes

If the bars on the bar-chart are filled with colour then the tics on the axis are no longer visible.

view help about using the 'histograms' plotting style

 help histograms

see help about fillstyles for use with boxes and histograms

 help set style fill

set the gap between bars to be equal to the width of the bar

 set style histogram clustered gap 1

The histogram bar is centered over the tick

narrow the gap between bars by increasing the boxwidth

 set style histogram clustered gap 1; set boxwidth 1.5
 se sty histog clustered gap 1; set boxwidth 1.5      abbreviated

plot data using filled bars with the bars closer together

  (echo "
    set style histogram clustered gap 1; 
    set style fill solid
    set boxwidth 1.7; set yrange [0:13]
    set xtics rotate by -45; 
    plot '-' using 2:xticlabels(1) with histograms";
    echo -e "
      bill 10\nbob 12\njack 3\njuan 6") | gnuplot -persist

plot data using unfilled histograms

  (echo "
    set style histogram clustered gap 1; 
    set xtics rotate by -45; 
    plot '-' using 1:xticlabels(2) with histograms";
    du -s ~/*|head -15) | gnuplot -persist

The fill patterns depend on the type of output wanted

histograms filled with a check

   (echo "
     set style histogram clustered gap 1
     set style fill pattern 1; 
     set xtics rotate by -45;
     plot '-' using 1:xticlabels(2) with histograms"; 
     du -s ~/* | head -15) | gnuplot -persist

histograms filled with slanted lines widely spaced

   (echo "
     set style histogram clustered gap 1
     set style fill pattern 4; 
     set xtics rotate by -45;
     plot '-' using 1:xticlabels(2) with histograms"; 
     du -s ~/*|head -15) | gnuplot -persist

the same as previously but create an image file o.png

  (echo "set terminal png; set output 'o.png';
   set style fill pattern 4; 
   set xtics rotate by -45;
   plot '-' using 1:xticlabels(2) with histograms"; 
   du -s ~/*|head -15) | gnuplot; display o.png

 set terminal png; set output "out.png"; plot sin(x)

histograms filled with slanted lines narrowly spaced

   (echo "set style fill pattern 6; 
    set xtics rotate by -45; 
    plot '-' using 1:xticlabels(2) with histograms"; 
    du -s ~/* | head -15) | gnuplot -persist

make a narrower gap between each bar of the histogram

  (echo "set style histogram clustered gap 1; 
   set boxwidth 1.5; set xtics rotate by -45; 
   plot '-' using 1:xticlabels(2) with histograms"; 
   du -s ~/* | head -15) | gnuplot -persist

set the bars to be filled with solid colour

 set style fill solid 2; plot 'data.txt' u 1:2 w histograms

plot data from standard in, solid red bars

  (echo "
     set style histogram clustered gap 1
     set style fill solid 1
     set xtics rotate by -45
     plot '-' using 1:xticlabels(2) with histograms"; 
     du -s ~/*|head -15) | gnuplot -persist

solid but pale red bars (intensity 0.3) close together

  (echo "set style fill solid 0.3;
     set style histogram clustered gap 1; 
     set boxwidth 1.5; set xtics rotate by -45; 
     plot '-' using 1:xticlabels(2) with histograms"; 
     du -s ~/* | head -15) | gnuplot -persist

solid but pale red bars (intensity 0.3) close together

  (echo "set style fill solid 0.3;
     set style fill border linetype 2;
     set style histogram clustered gap 1; 
     set boxwidth 1.5; set xtics rotate by -45; 
     plot '-' using 1:xticlabels(2) with histograms"; 
     du -s ~/* | head -15) | gnuplot -persist

pale red bars, x-labels rotated

  (echo "
     set style fill solid 0.3;
     set xtics rotate by -45; 
     plot '-' using 1:xticlabels(2) with histograms"; 
     du -s ~/* | head -15) | gnuplot -persist

fill each bar with colour half the intensity of the border colour

 set style fill solid 0.5; plot 'data.txt' u 1:2 w histograms

fill each bar with 'pastely' (faint) colour without any borders on the bars

 set style fill solid 0.5 noborder; plot 'data.txt' u 1:2 w histograms

plot column 1 vs column 2 of 'data.txt' with separated 'bars'

 plot 'data.txt' using 1:2 with histograms
 plot 'data.txt' u 1:2 w histo                     the same
 set style data histograms; plot 'data' using 1:2  the same

use labels with multiple columns

 plot 'file.dat' using 2, '' using 4, '' using 6:xticlabels()

plot text vs number data as a barchart with a y-axis range starting at 0

 plot [][0:] 'data.txt' u 2:xticlabels(1) w histograms

plot col 2, col 3 as histograms clustered around the labels of field 1

 plot 'data.txt' u 2:xticlabels(1) w histog, '' u 3 w histog

Bar Charts Using Boxes ‹↑›

plot a barchart with filled boxes (fill intensity 0.7)

 plot "test.dat" usi 1:2 w boxes fs solid 0.7
 plot "test.dat" using 1:2 w boxes fillstyle solid 0.7   the same

create a barchart with boxes taking up half the possible space

 set boxwidth 0.5; plot 'test.dat' using 1:2 with boxes  boxes separated
 set boxwidth 0.5; plot 'test.dat' u 1:2 w boxes         the same
 set boxw 0.5; plot 'data.txt' u 1:2 w boxes             the same

make thin filled rectangles for the bar chart

 set boxw 0.2; plot 'data.txt' u 1:2 w boxes fs solid 0.7

Bar Charts With Impulses ‹↑›

use lines from the axis to the value ('impulses') for graphing the data

 plot "data.txt" u 2:xticlabels(1) w imp
 plot "data.txt" using 2:xticlabels(1) with impulses  the same

The Datafile Format ‹↑›

The text datafile normally consists of rows of data where each row consists of a set of numbers separated by space or tab characters.

If a line begins with '#' is normally ignored

set the character which indicates that data is missing in the data file. $>> set datafile missing "-"

show what character currently indicates missing data in the text file $>> show datafile missing there is no default missing data character

set the character which separates fields in the datafile to a comma ','

 set datafile separator ','

Plotting Only Some Of The Data ‹↑›

plot only every second line from the text data file 'test.dat'

 plot "test.dat" every 2

plot only every second data block from the data file

 plot "test.dat" every :2  the datablocks are separated by blank lines

more examples

every I:J:K:L:M:N I Line increment J Data block increment K The first line L The first data block M The last line N The last data block every 2 plot every 2 line every ::3 plot from the 3-rd lines every ::3::5 plot from the 3-rd to 5-th lines every ::0::0 plot the first line only every 2::::6 plot the 1,3,5,7-th lines every :2 plot every 2 data block every :::5::8 plot from 5-th to 8-th data blocks ,,,

use a shell command to select or modify data to plot

 plot "< head -10 test.dat" using 1:2 with lines
 plot "< tail -3 test.dat" using 1:2 with lines
 plot "< head -5 test.dat" using 1:2 with lines,\
> plot "< tail -5 test.dat" using 1:2 with points ,,,

Calculating The Data ‹↑›

plot data from 'data.txt' doing arithmetic on each column

 plot 'table.dat' using ($3/$1):($2*134.44)

plot field 1 versus the square root of field 2

 plot "test.dat" using 1:(sqrt($2)) with points

Plotting Time Data ‹↑›

Plotting time data may be somewhat tricky, since it is necessary to inform gnuplot of the format of the time stamp contained in the data file

display the gnuplot help for the 'timefmt' setting

 echo help set timefmt | gnuplot

 help time/data
the data file 'data.txt'

10-Jun-04 90.23 9-Jun-04 89.90 8-Jun-04 88.64 7-Jun-04 88.75 4-Jun-04 87.95 3-Jun-04 87.85 ,,,

indicate that the x-axis data is time

 set xdata time

The dates in the file look like '10-Jun-04'

 set timefmt "%d-%b-%y"

On the x-axis, we want tics like Jun 10

 set format x "%b %d"

 plot ["31-May-04":"11-Jun-04"] 'data.txt' using 1:2 with linespoints

a clustered histogram with time data

10-Jun-04 90.23 90.75 89.89 90.46 9-Jun-04 89.90 90.55 89.81 90.09 8-Jun-04 88.64 90.50 88.40 90.04 7-Jun-04 88.75 88.99 88.01 88.64 4-Jun-04 87.95 88.49 87.50 87.56 3-Jun-04 87.85 88.10 87.35 87.35 ,,,

Multiple Graphs On One Field ‹↑›

plot the curves x-squard and x-cubed on the same grid (field)

 echo 'plot x**2, x**3' | gnuplot -persist

plot fractional powers of 'x' using bash brace expansion from x=0 to 4

 echo plot [0:4] "x**2."{1..7}"," "x**2.8" | gnuplot -persist

plot fractional powers of 'x' using bash brace expansion from x=0 to 4

 echo plot [0:4] "x**2."{1..7}"," "x**2.8" | gnuplot -persist

plot a long polynomial using bash brace expansion

 echo plot "x**"{9..1}" +" 1 | gnuplot -persist

Output Formats ‹↑›

view all possible output formats which gnuplot can produce

 echo set terminal | gnuplot
 echo help set terminal | gnuplot

create a 'png' image file 'out.png' with the graph of a sin curve

 set terminal png; set output "out.png"; plot sin(x)

plot graphs to a linux window (using wxWidgets tool kit)

 set terminal wxt

histogram styles
set style histogram clustered - put the bar right over the tic
set style histogram errorbars {gap <gapsize>} {<linewidth>}
set style histogram rowstacked
set style histogram columnstacked

create a png image 'o.png' graph of a sin curve in the current folder

   echo " 
     set terminal png; 
     set output 'o.png'; 
     splot sin(x**2 + y**2)/(x**2 + y**2)" | gnuplot
     display o.png

create a jpeg image 'o.jpg' a hyperbolic tangent function

   echo "
     set terminal jpeg
     set output 'o.jpg'
     set grid
     plot tanh(x)" | gnuplot
     display o.jpg

create a jpeg image 'o.jpg' of a polynomial with a value grid

   echo "
     set terminal jpeg
     set output 'o.jpg'
     set grid; plot x**3+x**2+x+1" | gnuplot
     display o.jpg

create postscript file 'o.eps' graph of a sin curve

 echo "set terminal eps; set output 'o.eps'; plot sin(x)" | gnuplot

You could include this in a report using the enscript tool to convert a text document to pdf or postscript format

Svg Outputs ‹↑›

Svg stands for scalable vector graphics and is an important image format because it stores the image as drawing 'instructions' in a plain text (xml) format. This means that it is possible to use the normal unix toolset to pre- or post-process svg images.

display the gnuplot help for the 'svg' output format

 echo help set terminal svg | gnuplot

Ascii Plottings ‹↑›

By setting the 'terminal' type to 'dumb' it is possible to produce a graph using only 'ascii' characters (normal letters numbers and symbols). This may be useful for including in text documents.

The 'persist' option to gnuplot is not necessary with the 'dumb' terminal because the graph is printed to 'standard out' (the screen) in anycase.

display the gnuplot help for the 'dumb' (ascii/text) output format

 echo help set terminal dumb | gnuplot

plot the graph 'sin(x)' as ascii text

 echo "set terminal dumb; plot x**2" | gnuplot
 echo "se te du; p x**2" | gnuplot the same

plot a parabola in plain text, width 60 characters and height 30 chars

 echo "set terminal dumb 60 30; plot x**2" | gnuplot

plot an 'ascii' parabola, 70x20 characters with no values on the axes

    echo "
      unset tics
      set terminal dumb 70 20 
      plot x**2" | gnuplot

plot an 'ascii' sin curve, 70x20 characters, this looks wobbly

 echo "set term dumb 70 20; p sin(x)" | gnuplot

plot an 'ascii' parabola, with a multiline title

    echo '
      set title "parabola\nx^2" 
      set terminal dumb 70 20 
      plot x**2' | gnuplot

Vim And Gnuplot ‹↑›

It is possible to create some new commands and command mappings to automatically generate graphs from the lines in a text file (be it data or gnuplot commands)

a vim command to plot the current line (assuming it is a gnuplot command)

 .w !sed 's/^ *#//;s/ \#.*$//' | gnuplot -persist

a 'mapping' to plot the current line (as above)

 :map! ,gp .w !sed 's/^ *#//;s/ \#.*$//' | gnuplot -persist

a new vim command to plot the current line

 :command! Gp .w !sed 's/^ *#//;s/ \#.*$//' | gnuplot -persist

a vim command to plot the current line to the file 'o.jpg'

 :.w !sed 's/^ */set terminal jpeg; set output "o.jpg";/' | gnuplot

a command to plot to an image to a given file name in the image folder

 command! -nargs=1 Glti .w !sed 's/^ */set terminal jpeg; set output "image\/<args>.jpg";/' | gnuplot; gthumb image/<args>.jpg

The new vim command above can be executed with

 :Glti test

Useful Plots ‹↑›

plot number of logon times for the last few days

  (echo "
    set xtic rot by -45; 
    set style data histogram;  
    plot [][0:] '-' using 1:xticlabels(2) ";
    last reboot|grep reboot|tr -s ' '|cut -d' ' -f5-7|\
      tr ' ' .|uniq -c) | gnuplot -persist

Plotting From The Command Line ‹↑›

plot a sin curve, from x=-20 to 20, from a bash shell, leaving plot window open

 echo 'set xrange [-20:20]; plot sin(x)' | gnuplot -persist
gnuplot displays the graph in a separate window, which it leaves open-persist keeps the plot window open after gnuplot exits

run gnuplot in batch mode with a command file

 echo 'set grid; plot sin(x)' > gp.txt; gnuplot -persist gp.txt
 echo 'set grid; plot sin(x)' > gp.txt; echo 'load gp.txt' | gnuplot -persist

Quickly graph a list of numbers

 gnuplot -persist <(echo "plot $(<(sort -n listOfNumbers.txt)) with lines")

an example of creating a graph with gnuplot

 http://www.pixelbeat.org/docs/web/access_log/analyzing.html

SURFACE OR 3D GRAPH PLOTTING

The gnuplot command 'splot' is capable of plotting 'surfaces' and other three-dimensional graphs

show some help for splot

 echo help splot | gnuplot
 echo help splot overview | gnuplot

 echo help isosamples | gnuplot

some important output formats for gnuplot
svg - scalable vector graphics (if you want to resize alot)
png - image format good for web-pages
jpg - image also good for web-pages
x11 - unix/linux window
wxt - linux window
gif - good for animations

plot a surface with double the number of surface lines

   echo "
     set isosamples 20
     splot x*x-y*y" | gnuplot -persist

plot a surface with colored lines

 echo "splot x*x-y*y with line palette" | gnuplot -persist

plot a surface with colored surfaces

 echo "splot x*x-y*y with pm3d" | gnuplot -persist

plot the same color surfaces but at different altitudes

 echo "splot x*x-y*y with pm3d, x*x+y*y with pm3d at t"|gnuplot -persist

plot a surface with doubled surface lines and x and y ranges -4 to 4

   echo "
     set isosamples 20
     splot [-4:4][-4:4] sin(x)-sin(y)" | gnuplot -persist

The 'set hidden' option hides the contour lines on the surface which are 'behind' other lines. This looks more natural (since one cant see something which is behind something else)

plot a surface with quadrupled surface lines and hidden lines hidden

   echo "
     set hidden
     set isosamples 40
     splot [-4:4][-4:4] sin(x)-sin(y)" | gnuplot -persist

Mathematical Surfaces ‹↑›

This section concentrates on those renderings of mathematically functions of 2 variables such as z = x^2 + y^2

show no axis values, extra contour, and no 'hidden' lines

    echo  "
     set hidden 
     set isosamples 40 
     splot x*x-y*y" | gnuplot -persist

Animations Of Graphs ‹↑›

By creating a large set of similar graphs with gnuplot we can then combine them into an animation using image magick 'convert'. This may be useful for showing trends etc.

create lots of similar graphs and create an animation, 10 frames/sec

  for i in {1..30}; do
   echo " 
     set terminal png; set output \"o${i}.png\"
     set hidden; set isosamples 40;
     splot [-4:4][-4:4] 0.${i}*sin(x)-sin(y)" | gnuplot; 
  done
  convert -delay 10 -size 200x200 o*.png -loop 0 -monitor test.gif

create an animation with a z range, 10 frames per second

  for i in {1..50}; do
   echo " 
     set terminal png; set output \"o${i}.png\"
     set hidden; set isosamples 40;
     splot [-4:4][-4:4][-0.5:0.5] sin(x*x+y*y)/(x*x+y*y+${i})" | gnuplot; 
  done
  convert -delay 10 -size 200x200 o*.png -loop 0 -monitor test.gif

It may be a good idea to keep all the ranges fixed so that the animation doesnt 'jump' around. Also the images need to be sorted into a numerical order before supplying to converted.

create an animation of a coloured surface rising, 10 frames per second

  for i in {1..50}; do
   echo " 
     set terminal png; set output \"${i}oo.png\"
     set hidden; set isosamples 40;
     splot [-6:6][-6:6][-20:60] x*x-y*y+($i) with pm3d" | gnuplot; 
  done
  convert -delay 10 -size 200x200 $(echo *oo.png|tr ' ' '\n'|sort -n) -loop 0 -monitor test.gif

use the following to sort the images before converting to an animation

 echo *oo.png | tr ' ' '\n' | sort -n

a nice animation of a wavy 3d surface rising

  for i in {1..30}; do
    echo  "
     set terminal png; set output \"${i}xx.png\"
     set border 0; set hidden; unset tics; unset key
     set isosamples 80 
     splot [-4:4][-4:4] sin(x*x+y*y)/(x*x+y*y+$i/2) w line palette"|gnuplot
  done
  convert -delay 10 -size 200x200 $(echo *xx.png|tr ' ' '\n'|sort -n) -loop 0 -monitor test.gif

Graphs As Art ‹↑›

Apart from the use of graphs and charts for information purposes they may also have an artistic and aesthetic value. One technique it to place many curves on the same graph to achieve an artistic effect.

place an number of sin curves on a graph for artistic effect

 echo "p [][-20:20] sin(x),sin(x)+1,sin(x)+2,tan(x)"|gnuplot -persist

use bash brace expansion to create many curves with gnuplot

 echo p "sin(x)+"{0..20}"," "tan(x) " | gnuplot -persist

use bash brace expansion to create many curves with gnuplot

 echo p "sin(x)+"{-15..15}"," "tan(x) " | gnuplot -persist

plot a sin and a cos curve on one field with different curve types

 echo "unset tics; unset key;p sin(x),cos(x) w boxes"|gnuplot -persist

plot a sin and a cos curve on one field with different curve types

 echo "unset tics;unset key;p" " sin(x)+"{0..5}"," "cos(x) w boxes"|gnuplot -persist

Artistic Surfaces ‹↑›

By increasing the number of contour lines (with isosamples) and removing numbers from the axis and legends, the surface becomes more of an aesthetic artifact rather than an informational one.

show no axis values and extra contour lines for a surface

    echo  "
     unset tics; unset key
     set isosamples 40 
     splot x*x-y*y" | gnuplot -persist

show no axis values, extra contour, and no 'hidden' lines

    echo  "
     set hidden; unset tics; unset key
     set isosamples 40 
     splot x*x-y*y" | gnuplot -persist

show no axis values, extra contour, and no 'hidden' lines

    echo  "
     set hidden; unset tics; unset key
     set isosamples 40 
     splot x*x+y*y" | gnuplot -persist

remove all axes and values and plot with height colours

    echo  "
     set border 0; set hidden; unset tics; unset key
     set isosamples 80 
     splot [-4:4][-4:4] sin(x*x+y*y)/(x*x+y*y) with line palette" \
       | gnuplot -persist

Some People ‹↑›

www: Colin
Kelley and Thomas Williams original developers of gnuplot in 1986
www: David
Kotz developed a version of gnuplot for tex output
DOCUMENT-NOTES:
important options
set isosamples 20 - increases the number of 'contour' lines by 2
set hidden - does not draw contour lines which are behind others