Create scientific plots using gnuplot

April 19th, 2011 | 3 Comments

Often we use a specific line color or output terminal in Gnuplot. Or we are not satisfied with one of the default settings, for example the font used by the png terminal. There exists an easy way to fix these settings. Gnuplot reads a startup file, called .gnuplot under Linux and GNUPLOT.INI under Windows. First it searches the current directory and then your home directory after that file.

Here is an example of what is possible to include in the startup file.

# enable macros
set macros
# search for functions or data files in these directories
set loadpath '/usr/local/lib/gnuplot'
# add default line colors
set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 5   # blue
set style line 2 lc rgb '#dd181f' lt 1 lw 2 pt 7   # red
# add macros to select the desired line style
BLUE = "1"
RED = "2"
# add macros to select a desired terminal
WXT = "set terminal wxt size 350,262 enhanced font 'Verdana,10' \
PNG = "set terminal pngcairo size 350,262 enhanced font 'Verdana,10'"
SVG = "set terminal svg size 350,262 fname \
   'Verdana, Helvetica, Arial, sans-serif' fsize = 10"

With these settings and the use of macros our plotting will become more easier. For example to plot the sinusoid from the plotting functions introduction we can now use the following code.

plot f(x) title 'sin(x)' with lines ls @BLUE, \
     g(x) notitle with lines ls @RED

Note that you have to omit the usage of reset in your code, because it will clear the line style settings.


Fig. 1 Plotting using the configuration file (code to produce this figure)

You can of course override the style settings of the configuration file.

plot f(x) title 'sin(x)' with lines ls @BLUE, \
     g(x) notitle with lines ls @RED lw 1

Fig. 1 Overwriting settings from the configuration file (code to produce this figure)


  1. Saurabh says:

    I am in linux and i am using .
    G N U P L O T
    Version 4.6 patchlevel 1
    In my home folder there is a file ~/.gnuplot-wxt .
    But I am not these line .
    I giving me an error .
    If i add a line
    “set macros” #(without “)
    It is showing some error like this
    04:33:36 IST: file ‘/home/saurabh/.gnuplot-wxt’, line 9: ‘=’ expected.
    04:33:36 IST: file ‘/home/saurabh/.gnuplot-wxt’, line 10: ‘=’ expected.
    help me .

  2. hagen says:

    Hi Saurabh,

    I’m afraid you have to post the whole file in order to help you.

  3. Panagiotis says:

    since is the first time I leave a comment, congratulations and thank you for your wonderful and helpful site.

    I just wanted to add a new use of linetypes
    If someone uses linetypes instead of style line where
    “Unlike line styles, redefinitions by set linetype are persistent; they are not affected by reset.”


    (Y.G.I know that this article is “old”, but since now we can use this feature, I thought if someone reads this article, it will be good for him to know it)