April 27th, 2010 | 11 Comments
In this tutorial you will create your first gnuplot figure. Therefore open a text file in your preferred text editor, or directly download the code for this tutorial.
Firstly we have to tell gnuplot the output terminal we want to use. The most common are png, svg, postscript or LaTeX output, but first we will start with the output just on the screen using the wxWidgets toolkit. Therefore we have to write this line of code:
set terminal wxt size 350,262 enhanced font 'Verdana,10' persist
After that we will specify the style of the output, because the default gnuplot output is ugly in many ways.
# Line width of the axes set border linewidth 1.5 # Line styles set style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 2 set style line 2 linecolor rgb '#dd181f' linetype 1 linewidth 2
You can see, a comment starts with a
#. We use the
set command and specify the border line width and two line styles with the number
2. For them we specified a colour, the line type where 1 is a normal line and also the line width.
In our first graph we want to plot a sinusoid and a cosinus. Therefore we specify our functions and plot them:
a = 0.9 f(x) = a * sin(x) g(x) = a * cos(x) # Plot plot f(x) title 'sin(x)' with lines linestyle 1, \ g(x) notitle with lines linestyle 2
As you can see, the definitions of functions in gnuplot are straight forward. We want to plot more than one function that’s why we have to divide the two commands with a comma. The backslash tells gnuplot that we have a line break at this position.
Now we save our file as introduction.gnu and execute it by running the following command in BASH under Linux.
$ gnuplot introduction.gnu
Under Windows you can start gnuplot with the
wgnuplot.exe file and then plot the introduction file by the following command.
gnuplot> load 'path_to_your_file/introduction.gnu'
This should produce in both cases a graph as shown in Fig. 1.
As you can see, we have to tune it a little bit to get a nice graph. Therefore put the following code in front of the function’s definitions (in front of the plot command is sufficient, but I prefer to have the function’s definitions near the plot command).
# Legend set key at 6.1,1.3 # Axes label set xlabel 'x' set ylabel 'y' # Axes ranges set xrange [-2*pi:2*pi] set yrange [-1.5:1.5] # Axes tics set xtics ('-2π' -2*pi, '-π' -pi, 0, 'π' pi, '2π' 2*pi) set ytics 1 set tics scale 0.75
At first we set the legend to a specific position, put labels on the axes and set the ranges of the axes. After that, we specify the positions of the xtics and set strings (note that we can directly use utf-8 strings here) to the given positions. For the ytics we tell gnuplot to make a tic every multiple of 1. Also, we scale the length of the tics, because the default length is a bit too long. Now we can save the file, run the above command again and hopefully get the following figure.
As you have seen in this short tutorial, plotting a function with gnuplot is easy, you only have to tweak a little bit on the appearances of the figure.