Gnuplotting

Create scientific plots using gnuplot

April 27th, 2010 | 12 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 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:

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.

Ugly sinusoid

Fig. 1 Our first ugly result (code to produce this figure)

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.

Sinusoid

Fig. 2 Our first very nice result (code to produce this 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 use other outputs for the figures (png, svg, latex) than your screen.

12 Comments

  1. greg says:

    I would love to see a page on splot with pm3d including colors.

  2. dbanj says:

    Thanks!

  3. Matthias says:

    Very nice presented and very informative. Keep on goin…

  4. Jong Hyun says:

    Thanks. When I plot using script, the plot appears and in a flash it disappears. I use ubuntu linux. It may not happen in other environments To make plot remain, I add one line:

    pause -1

    at the end of the script. Then it pauses until I type a key in terminal, or quit the plot window. I hope it helps.

  5. hagen says:

    Hi Jong Hyun. I assume that you are trying to use the wxt terminal. With this terminal and the persist option your problem should not occur. Here is the terminal line from the script:

    set terminal wxt size 350,262 enhanced font 'Verdana,10' persist
    
  6. Fouad says:

    Thank you very much.
    I like the way you present and the colors are fantastic.i would be using gnuplot more and more :)
    keep it up

  7. Rami says:

    Hey and thanks for this site,

    I’m used to work with Matlab but nowdays I have to do some fortran work and it seems that gnuplot may give me nice results.

    My problem is that I have to do all this styling over and over and over and over, RGB especially is tedious. Can I use default file for all of these options? Do you have a link for such a file?

    Also, apart from the 3 tutorials on this site, would you recommand some other book / site?

    Thanks Again,
    Rami

  8. hagen says:

    Hi Rami.

    You can use the main gnuplot configuration file for storing your settings. Otherwise you can create small files storing only one particular thing, like the color definitions as color.cfg and then load them with

    load 'color.cfg'
    

    Here you can find some files with color definitions that you can directly load:
    https://github.com/Gnuplotting/gnuplot-palettes
    For example

    load 'jet.pal'
    

    I learned gnuplot basically with the Not so FAQs site.

  9. Rami says:

    Thanks for your response. I’ll try to get going with it.

    By the way, do you think I should bother learn gnuplot or matplotlib in python?

    Rami

  10. hagen says:

    Hi Rami.

    That is a good question. I have used python very rarely so far, and not really for plotting. Matplotlib is definitely a good option, especially if you start learning. But of course I would always propose to learn gnuplot ;)

  11. julian says:

    I want to plot a wave, for example, ‘plot cos(w*t + kt)’ but I don’t know how to do it.

    is easy when you have just one parameter, but in this case is diferent.

    thanks

  12. Manohar says:

    how to plot complex number equation : cc = A/(1+i*w*t); where A= const, w=variable, t=const
    kindly suggest, as the equation has real and imaginary parts