Gnuplot is a powerful scientific plotting program. In fact is more powerful than people usually think first time time they use it. May be this is because it has no GUI. Instead of that, you have to type commands in a plain boring terminal.
Well, in this post I'm going to put an example of how to perform a relatively complex graph, using greek letters and legend. Of course, everything I use here is explained perfectly in the help.
Gnuplot has several output formats (called terminals). Depending on what you are going to do later with the output you get from the program, you can use one or another (from raster formats like png of jpeg, up to vectorial ones such as eps or svg). In general I advice to use the terminal postscript, and later on to export the file to a raster format like png using a images manipulation program like Imagemagick. I also advice to use the options: eps (to get encapsulated postscript code), color (to have colors enabled) and enhanced (to be able to use advanced postscript options such as greek letters).
To get a greek letter, you have to use the postscript terminal, with the option enhanced. Then, to add the alpha greek letter, you have to type {/Symbol a}. Analogously for other letters, changing the "a" by other, like "D" for Delta and so on...
Finally, the thing I like most about Gnuplot is that it's easily scriptable. Here there is a simple script which shows all these ideas in practice (comments are added to make code more readable):
#!/bin/bash
gnuplot << TOEND
# Setting the terminal postscript with the options
set terminal postscript eps color enhanced "Helvetica" 20
# Setting the output file name
set output 'plot.eps'
#Setting up the grid and labels
set grid
set title "A simple graphic without LaTeX"
set xrange [-pi:pi]
set xlabel "Angle {/Symbol a}"
set ylabel "{/Symbol Dw}"
set ytics 0.5
set xtics ("{/Symbol p}" -pi, "{/Symbol p}/2" -pi/2, "0" 0, "{/Symbol p}/2" pi/2, "{/Symbol p}" pi)
# Setting the legend
set key horizontal below height 2
set key box lt 2 lc -1 lw 3
# The plot itself (\ is to broke lines)
set size 1,1
plot sin(x)**2 w l lt 1 lc 1 lw 3 t "sin^2({/Symbol a})", \
sin(2*x)/x w l lt 2 lc 3 lw 3 t "sin(2{/Symbol a})/{/Symbol a}", \
sin(x) w l lt 3 lc 2 lw 3 t "sin{/Symbol a})"
TOEND
convert -density 150 plot.eps plot.png
rm plot.eps

1 Comment:
Thank you for this quick and efficient example!
--GKA
Post a Comment