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:
After that we will specify the style of the output, because the default Gnuplot output is ugly in many ways.
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 1
and 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:
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.
Under Windows you can start Gnuplot with the
wgnuplot.exe
file and then plot the introduction file by the following command.
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).
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.
Now you can have a look at how to plot data from a data file or how to useother outputs for the figures (png, svg, latex) than your screen.
No hay comentarios:
Publicar un comentario