Gnuplot: Sine Wave

Rather than explain every detail of every command, which is already available in gnuplot's detailed help system, I've just given a general description of the essential commands. This should enable you to get the gist of what's going on quickly; you can fill in any details by referring to the help system (at the gnuplot command line just type help followed by the particular keyword you're interested in).

The following script generates the sine wave shown below in Figure 1.

The first couple of commands clear everything up from previous runs. Next, we turn off the key (or legend) since it would add clutter to what is otherwise a very simple graph. Then we add an overall title for the graph.

To plot one full cycle of a sine wave, X must go from zero to 6.268 (or 2pi) since gnuplot assumes you mean radians when you use trigonometric functions. Gnuplot's format for ranges places them within square brackets, with the upper and lower limits separated by a colon.

xlabel allows us to set our own label for the X axis. We also want our own tick marks at the main points along the X axis. We can insert special Greek symbols using the {/Symbol x} command, where x is a character that selects the required symbol. A list of these symbols can be found in the file ps_guide.ps that comes with the gnuplot distribution. The letter “p” gives us the symbol for Pi. Note, also, that we can use expressions in gnuplot strings, so instead of using “1.57”, we can use “pi/2”

In order for the Greek symbols to be produced, the terminal's "enhanced" option must be used. This is done in the set terminal command.

The format of the xtics string is thus: the entire string is contained in round brackets, which contains a list of pairs separated by commas. For each pair, the first item is a string with the text you want to appear at this tickmark, whilst the second item is a number that indicates where along the X axis this tickmark should appear. The backslash you can see in the xtics definition is gnuplot's line-continuation character.

To reiterate, the first item in each pair is the symbol to display at that xtic (marker on the x-axis), whilst the second item is the x position at which to display that symbol. The curly brackets contain special PostScript commands; in this case, the Greek symbol for pi.

The xzeroaxis command draws a horizontal line along the zero value on the Y axis (parallel to the X axis).

The Y axis is left completely unlabelled (the default) and unticked (unset ytics), since we're only interested in the shape of the wave's amplitude.



# Clear any previous plots.
clear

# Reset all plotting variables to their default values.
reset

# We don't need a key (or legend) for this simple graph.
set key off

# Set the title for the graph.
set title "Sine against Phase"

# We want the graph to cover a full sine wave.
set xrange [0:6.28]

# Set the label for the X axis.
set xlabel "Phase (radians)"

# Set the tick-marks for the X-axis. Use the Postscript
# symbol for Pi.
set xtics ("0" 0,"0.5{/Symbol p}" pi/2, "{/Symbol p}" pi, \
	"1.5{/Symbol p}" 1.5*pi, "2{/Symbol p}" 2*pi)

# Draw a horizontal centreline.
set xzeroaxis

# Pure sine wave amplitude ranges from +1 to -1.
set yrange [-1:1]

# No tick-marks are needed for the Y-axis .
unset ytics

# The wxt terminal is the default, but we need to turn on
# the enhanced feature in order to get the Greek symbols.
set terminal wxt enhanced

# Plot the curve.
plot sin(x)
Figure 1: Sine wave graph

Figure 1: Sine wave graph


Home About Me
Copyright © Neil Carter

Content last updated: 2012-02-16