About instructions

You don't need any prior experience with Gnuplota or the terminal to read this manual, and you can use Windows. Instructions will guide you through Gnuplots from the beginning to more advanced fads. It is brief, but I recommend, in parallel with reading the commands, to try on your PC - the instructions are so written.

What is Gnuplot?

Gnuplot also works as a terminal program on Linux. It is installed using a command, similar to sudo apt-get install gnuplot (by distra) and works just as it does on Windows.

Fence command

Type the following code tentatively into the terminal and press enter:

plot x**2

Do you see that a window has appeared with a graph of the quadratic function $x^2$, which GnuPlot plotted on the graph something like this:

graf kvadratické funkce v Gnuplotu

You may notice several buttons on the top bar, the most important of which is the grid button with which you can load the grid (draw a square network in the chart). This can be replaced with a set grid command and turned off using the unset grid. With the second button from the left, you can save the chart as a picture. I won't describe the other buttons, you can try them yourself. So the fence command plots into the graph what we tell it, what we write next (or what's in the fence command argument). We can render the functions $\sin()$, $\cos()$, $\exp()$ etc., but remember that GnuPlot uses a decimal point (not a comma!) and the power is given not by a roof (^), but by a double multiplication (**). It is also possible to render multiple functions at once, separated by a comma, like this:

plot sin(x), cos(x)

To Edit a Chart

Sometimes the graph of the function we've drawn doesn't match our requirements. For example, the command-induced function plot sin(x)might not be as nicely displayed as we would like, plus the axis labels are missing and the legend is not pretty. Now let's see how we can change that.

If you save these commands to your .gn file, then you can only load the commands with the load \path\k\file.gnu\ (or simply drag the file into the window and press enter). Note: in Gnuplot, you can move between directories of a classically Unix cd. Commands:

set ylabel "y"
set xlabel "x"
set xrange [0:6.5]
set xtics 0.5*3.14159
set title "Sinusoida"
plot sin(x) title "y=sin(x)"

Specifically, the following graph will emerge from these commands:

graf sinusoidy

Other commands I have not mentioned now, but might be useful:

How to Plot Data

You may want to bring your own data, perhaps from an experiment, into the graph. In this case, Gnuplot can help. You just have to create a file (ideally a .txt, I think you're also going to .csv) with the following dates:

#Popisekx Popiseky
Hodnotax1 Hodnotax2

Data can be separated by both a tab and a space. The cross # indicates a comment, what's behind it is ignored by Gnuplot. Then plot \path\k\file.txt\ will bring up data. If you can't find a file path, drag the file to Gnuplot and rewrite the load to the fence of the command that will be created there.

Test your data flow by trying to create the same chart as in one job from the correspondence seminar Exhaust. So let's get the data in the right format first (watch out, it has to have decimal dots):

#m/kg t/s
0.1 24
0.2 44
0.3 56
0.4 70
0.5 86
0.6 99
0.7 113
0.8 136
0.9 151
1.0 165

We then save the data to a file named data.txt (but it may be a different name) and then take it out using the plot \data.txt\. We then get this output:

graf naměřených dat, které jsou zhruba rozmístěné na přímce

Note: I'm sure you learned in school that when plotting measured data into a chart, no matter what, you can't connect points - measured values. However, if you do want to do this, just type: plot \data.txt\ with lines for the path to the file in the plot command.

How to Fit

You're probably going to have some data plotted out at some point, and you're going to want to fit it, or fit it through a function. This too can be done in Gnuplot. Let's take a look at the data from last Exhaust Task. I will give here a step-by-step guide to how this is done:

  1. First, you must estimate the shape of the function you want to interlace the chart. In this case, it's probably going to be a linear relationship, or $f(x) = a\cdot x+c$.
  2. Let's define the function in gnuplot by typing its prescription into the terminal -- f(x) = a*x+c.
    1. Note that the variable is $x$, but both $a$ and $c$ are parameters we don't know.
  3. Now the program must calculate all the parameters. So write:
    fit f(x) "data.txt" via a, c
    1. The program will list calculated fitness constants that may come in handy.
  5. Finally, we plot both the function f(x) and the data in the graph as follows:

plot 'data.txt' title 'naměřená data', f(x) title 'lineární fit'

This should be the chart:

graf s daty i fitem

Of course, you can try to fit the data with other dependencies, but then when you command fit you have to specify all the parameters when via. You can find fitness insecurity in the program listing.

Important: gnuplot fits does with its own numerical algorithm. This algorithm only works if they convert the values of the numbers. It doesn't need to be converted if you enter a parameter where just a small change results in a large change in function. Maybe if we have a f(x)=a*(1+x/b), then if b is small, this fit is unstable. Better use the equivalent fit in that case f(x)=a*(b+x).

Other amenities of Gnuplot

  1. You can also draw 3D charts. All you have to do is have a data set with three columns, or a two-variable function. The splot function takes care of that. Try entering the following line to see:
    splot x**2+y**2
  3. Errorbars can also be drawn. All you have to do is have a data file with three columns where the third column is the error bars in the $y$ axis and write:
    plot “cesta\k\souboru.txt with yerrorbars
  5. You can select a color and line type for the fenced data. To do this, read the documentation, the basic color change is made by the following command (lt means line type and colour is some predefined color):
    plot sin(x) lt rgb "colour",
  7. set decimalsign ,
  8. set encoding utf8 for Czech characters
  9. set dt (number) changes dash type, which is good for black and white printing.
  10. set key spacing 1.5magnifies the legend to make it more readable.

How to write LaTeX axis labels

Gnuplot has various so-called terminals that indicate in which format the output will be. One is the LaTeX format, which comes in handy when you want Greek letters in the axis labels. It is used as follows:

set term epslatex standalone
set output "sample.tex"
plot x**2
set output

This creates a sample.tex and sample.eps file in the folder where you are operating. Now in the terminal, you must compile the file using pdflatex sample.tex. But you can also compile from gnuplot using

system('latex fig1.tex && dvips fig1.dvi && ps2pdf fig1.ps')

Of course, the resulting sample.tex can be compiled in an editor, but it's more convenient through the terminal. I strongly recommend using load \plot.gnu\ for this type of compilation.

Did the Gnuplot story help? Perhaps my guide to LaTeX will help, or you can read one of my articles for example on foreign language learning or differential calculus.