Plotting graphs with Gnuplot

Gnuplot Linux Windows

Graph plotting with Gnuplot can be quite difficult without some basic knowledge of the syntax.

I also find that some settings overwrite others, for an example see this post on Stackoverflow and the same applies in this script below where the second line overwrites the first, despite them supposedly being treated as separate lines.

set grid xtics ytics ls 102
set grid mxtics mytics ls 112

The examples below have been tested on Linux (Version 4.6 patchlevel 6) and Windows (Version 5 patchlevel 6)

They may not work on older (or even newer!) versions of gnuplot.

Gnuplot scripts accept the “#” character as a comment.

This is a fairly basic script to plot two columns of data – in this case Frequency and amplitude – and the X axis is a log scale.

For Windows Gnuplot: (Linux example below)

reset
set encoding utf8
set datafile separator ','
# ********** define colours ***********
set style line 101 lc rgb "#2F4F4F" # dark slate gray 
set style line 102 lc rgb "#ff0000" # red
set style line 103 lc rgb "#556b2f" # dark olive green
set style line 104 lc rgb "#0000ff" # blue
set style line 105 lc rgb "#999999" # gray60
set style line 106 lc rgb "#2e8b57" # sea green
set style line 107 lc rgb "#FFA500" # orange
set style line 108 lc rgb "#7F7F7F" # gray
set style line 110 lc rgb "#2F4F4F" # dark slate gray 
set style line 111 lc rgb "#000000" # black
set style line 112 lc rgb "#cccccc" # gray80
# ********** setup grids ***********
set terminal wxt font "Tahoma,10" size 1400,768 enhanced background rgb '#FFFFFF'
set grid
set logscale x 10
# border style and colour 
set border 4095 ls 111
# grid and minor grids
set grid xtics ytics ls 102
set grid mxtics mytics ls 112
# x-y legend and key colours
set key textcolor ls 102
set xtics textcolor ls 110
set ytics textcolor ls 110
set xtics norangelimit font "Tahoma,9"
set ylabel textcolor ls 104
set y2label textcolor ls 104
set xlabel textcolor ls 104
# ********* set label names **********
set xlabel "Frequency, Hz"
set ylabel "Amplitude, dBu"
set y2label "Amplitude dBu"
# ********* set graph X & Y ranges **********
set xrange [10:1000000]
set mxtics 10
set yrange [-25:10]
set mytics 5
set format x '%.0f'
# ********* set graph title **********
set title "Amplitude vs Frequency"
# ********* set sample count for smoothing (if required) *********
set samples 1000
# ********* datafile to plot *********
# the 'bezier' option will smooth the plot - there are other smoothing options
# plot 'c:\temp\plot.txt' u 1:2 w linespoints ls 1 smooth bezier t "Test 4V"
# or plot a sine wave
plot sin(x) ls 101 smooth bezier t "Sine"
set output
reset

For Linux, I usually send the graph output to a file as a PNG image.

This bash script will do exactly the same as above but output to a png file in “/var/www/html/gnuplot/sine.png”.

This is set with the line: set output "/var/www/html/gnuplot/sine.png"

If your Linux box has a gui, you can of course output to the gui as well (gnuplot-x11 required for this).

#!/bin/bash

export GDFONTPATH="/usr/share/fonts/truetype/ttf-dejavu/"
export GNUPLOT_FONTPATH="/usr/share/fonts/truetype/ttf-dejavu/"

/usr/bin/gnuplot << EOF

reset
set encoding utf8
# Data file uses semicolon as a separator
set datafile separator ','
# ********** output to file **********
set terminal png font "DejaVuSansCondensed,10" size 1400,768
# ********** output to gui **********
# set terminal wxt font "Tahoma,10" size 1400,768 enhanced background rgb '#FFFFFF'
# ********** define colours ***********
set style line 101 lc rgb "#2F4F4F" # dark slate gray 
set style line 102 lc rgb "#ff0000" # red
set style line 103 lc rgb "#556b2f" # dark olive green
set style line 104 lc rgb "#0000ff" # blue
set style line 105 lc rgb "#999999" # gray60
set style line 106 lc rgb "#2e8b57" # sea green
set style line 107 lc rgb "#FFA500" # orange
set style line 108 lc rgb "#7F7F7F" # gray
set style line 110 lc rgb "#2F4F4F" # dark slate gray 
set style line 111 lc rgb "#000000" # black
set style line 112 lc rgb "#cccccc" # gray80
# ********** setup grids ***********
set grid
set logscale x 10
# border style and colour 
set border 4095 ls 111
# grid and minor grids
set grid xtics ytics ls 102
set grid mxtics mytics ls 112
# x-y legend and key colours
set key textcolor ls 102
set xtics textcolor ls 110
set ytics textcolor ls 110
set xtics norangelimit font "Tahoma,9"
set ylabel textcolor ls 104
set y2label textcolor ls 104
set xlabel textcolor ls 104
# ********* set label names **********
set xlabel "Frequency, Hz"
set ylabel "Amplitude, dBu"
set y2label "Amplitude dBu"
# ********* set graph X & Y ranges **********
set xrange [10:1000000]
set mxtics 10
set yrange [-25:10]
set mytics 5
set format x '%.0f'
# ********* set graph title **********
set title "Amplitude vs Frequency"
# ********* set sample count for smoothing (if required) *********
set samples 1000
# omit the line below if outputting to gui
set output "/var/www/html/gnuplot/sine.png"
# ********* datafile to plot *********
# the 'bezier' option will smooth the plot - there are other smoothing options
# plot 'c:\temp\new-plot.txt' u 1:2 w linespoints ls 1 smooth bezier t "Test 4V"
# or plot a sine wave
plot sin(x) ls 101 smooth bezier t "Sine"
# Name our output file
# This is important because it closes our output file.
set output 
reset
# end of script
EOF

Finally... this _gnuplot_ script (Linux) will produce the graph as below:

reset
set encoding utf8
set terminal png font "DejaVuSansCondensed,10" size 800,600
set style line 101 lc rgb "#2F4F4F" # dark slate gray 
set style line 102 lc rgb "#ff0000" # red
set style line 103 lc rgb "#556b2f" # dark olive green
set style line 104 lc rgb "#0000ff" # blue
set style line 105 lc rgb "#999999" # gray60
set style line 106 lc rgb "#2e8b57" # sea green
set style line 107 lc rgb "#FFA500" # orange
set style line 108 lc rgb "#7F7F7F" # gray
set style line 110 lc rgb "#2F4F4F" # dark slate gray 
set style line 111 lc rgb "#000000" # black
set style line 112 lc rgb "#cccccc" # gray80
set grid
set border 4095 ls 111
set grid xtics ytics ls 102
set grid mxtics mytics ls 112
set key textcolor ls 102
set xtics textcolor ls 110
set ytics textcolor ls 110
set xtics norangelimit font "DejaVuSansCondensed,9"
set ylabel textcolor ls 104
set y2label textcolor ls 104
set xlabel textcolor ls 104
set xlabel "Time t"
set ylabel "Amplitude"
set y2label "Amplitude"
set xrange [-10:10]
set mxtics 10
set yrange [-2:2]
set mytics 5
set format x '%.0f'
set title "Sine Wave"
set output "/var/www/html/sine.png"
plot sin(x) ls 101 t "Sine Wave"
set output 
reset