Gnuplot

From MediaWiki

(Difference between revisions)
Jump to: navigation, search
(Axes range)
(Axes range)
 
(2 intermediate revisions not shown)
Line 161: Line 161:
   gnuplot>> print GPVAL_X_MIN, ":", GPVAL_X_MAX
   gnuplot>> print GPVAL_X_MIN, ":", GPVAL_X_MAX
-
For example, we can use this if we need to make two x-axes, both upper and lower but with different values. Say, we want to have some x-values both with the log10(x) on the upper axis. We can make it work in several steps:
+
For example, we can use this if we need to make two x-axes, both upper and lower but having different values. Say, we want to have both x on the lower and log10(x) on the upper axis. We can make it work in several steps:
   gnuplot>> plot 'file.txt'; set x2range[log10(GPVAL_X_MIN):log10(GPVAL_X_MIN)]; set x2tics border; replot
   gnuplot>> plot 'file.txt'; set x2range[log10(GPVAL_X_MIN):log10(GPVAL_X_MIN)]; set x2tics border; replot
Line 169: Line 169:
   gnuplot>>plot 'file.txt'; set x2range[10**GPVAL_X_MIN:10**GPVAL_X_MAX]; set x2tics border; set grid x2tics;replot
   gnuplot>>plot 'file.txt'; set x2range[10**GPVAL_X_MIN:10**GPVAL_X_MAX]; set x2tics border; set grid x2tics;replot
-
if we already have the logarithm and need normal x-scale on the upper axis. Here, '''set x2tics border;''' means DISPLAY the values on the graph. Otherwise, values are calculated, but not displayed. And additionally, grid is plotted using new numbers. One should be careful with these predetermined values, for the first graph could not be done if '''x < 0'''.
+
if we already have the logarithm and need normal x-scale on the upper axis. Here, ''set x2tics border'' means DISPLAY the values on the graph. Otherwise, values are calculated, but not displayed. And additionally, grid is plotted using new numbers. One should be careful with these predetermined values, for the first graph could not be done if '''x < 0''', and that is one good reason to control the limits. The other would be to have several data to compare at the end - it may be annoying if ranges are different, so we can 
 +
 
 +
2. Define both x- and y-range, which can be done simply by typing
 +
 
 +
  gnuplot>>xmin=0.28; xmax=0.4; set xrange[xmin:xmax]
 +
  gnuplot>>plot 'file.txt'; set x2range[10**xmin:10**xmax]; set x2tics border; 
 +
 
 +
for example. And likewise
 +
  gnuplot>>ymin=0.075; ymax=0.11; set yrange[ymin:ymax]

Latest revision as of 08:00, 30 July 2008

Very short manual for the beginners:

Contents

Useful starting notes

  • All commands may be used as usual from a gnuplot command-line starting with !
  gnuplot> !ls
  N.txt     Nerr0.1.txt  ab.par      fit.log      
  N0.1.txt  Nnew.txt     ab.par.old  fitNerr.log
  !
  gnuplot> !pwd
  /home/ana/Msc/Catalogs/Janknecht2006/Results5/Results0.1/N_z0.56-1.91
  !

But all other shell-supporting commands can be used in this way, too.

  • The next very useful thing to know is the command help
  gnuplot> help fit
  The `fit` command can fit a user-defined function to a set of data points
  (x,y) or (x,y,z), using an implementation of the nonlinear least-squares
  (NLLS) Marquardt-Levenberg algorithm.  Any user-defined variable occurring in
  the function body may serve as a fit parameter, but the return type of the
  function must be real.
  
  Syntax:
        fit {[xrange] {[yrange]}} <function> '<datafile>'
            {datafile-modifiers}
            via '<parameter file>' | <var1>{,<var2>,...}
  ...

for example, will explain how to fit the data with an example (help files are very self-explanatory).

  • His majesty the term. If we type only
  gnuplot> set term
  
  Available terminal types:
             aed512  AED 512 Terminal
             aed767  AED 767 Terminal
               aifm  Adobe Illustrator 3.0 Format
           bitgraph  BBN Bitgraph Terminal
  ...

we'll get a list of available formats for saving the results of our work (images, tables ...) and others. By default, the term is set to x11, i.e. the screen. All the work we make will be displayed, but not saved. The most useful terms which will save the work (in my opinion) are:

  • set term postscript
  • set term jpeg
  • set term table
  • But first, we should explore a bit the ways gnuplot can be used:

1. We can type all commands in a command line , which is far less efficient then

2. Making a .gnu file, a common text file written in a gnuplot language (using any text editor )

When we start gnuplot in a dir where we have 'data.txt', for example, a file containing two columns x y, we can simply type

  gnuplot> plot 'data.txt' u 1:2 ... plot data.txt using 1st column as x and 2nd as  y

and obtain a graph y=f(x), or we can make a .gnu file containing only one line of a text

plot 'data.txt' u 1:2; quit;

and save it as a test.gnu file and then, in a dir where the data are, simply type on a command-line

  ana@debeli:~> gnuplot test.gnu

The effect will be the same, except in the former case the screen will blink and die within a second. To prevent this from happening, we can add the following line in a test.gnu file:

!sleep 5

that will make gnuplot displaying our graph for 5 seconds and then die. The former is the better, for we can make complex requests that will be saved for some further use. And there is even a better way to do this - we can add

!read

instead of sleep which will make our graph display for as long as we wish, and we just need to press any key in the console to kill it. Now, if we want to save the graph in a .jpeg format, test.gnu should look like:

set term jpeg; set output 'test.jpeg'; plot 'data.txt' u 1:2; quit;

Another good thing related to .gnu files is the use of pipes for changing some details and than just running gnuplot to deal with this changes.

Example 1. We have a simple test.gnu file that plots some function, and in a same dir we have exp.txt:

a=0.25; b=0.02; c=0.05; d=0.1
f(x)=c/((x-a)**2+b)+d/sqrt(x)
set xrange [0:1]; set xtics 0.2; set mxtics 10
set yrange [0:4]; set ytics 0.5; set mytics 5
plot f(x) title 'lorenz' #, 'exp.dat' using 1:2:3 with yerrorbars
!read
quit

And we want to see changes caused by the change of parameter a form 0.25 to 1.5. We write a pipe in a command-line:

  ana@debeli:~> sed "s/a=0.25/a=1.5/g" test.gnu | /usr/bin/gnuplot

Example 2. We want to plot several files (in a same dir) that are all called exp*.txt:

!ls exp*.txt
exp.txt exp1.txt exp2.txt

And we want to see their graphs one after another, so we can track the changes caused by the change of some parameter:

  ana@debeli:~> for i in exp?.dat; do sed "s/exp.dat/`echo $i | sed "s/exp.dat/\$i/"`/g" test.gnu |
  /usr/bin/gnuplot ; done

But, this can become very annoying and confusing, when the pipe grows over several lines on screen (in the giant worm). In this case, we should

3. Do all this from a script, especially if we are dealing with several files, in different dirs and have the need for some additional programs (later we'll get back to this)

/*Although, we shouldn't use them just like this, but with some options we can control. For example if*/

Special Characters / Specijalni karakteri

A good 2-page manual for special signs can be found in ps_guide.ps

                    PostScript Character Codes
  T = text (here Times-Roman) S = Symbol Z = ZapfDingbats E = ISO Latin-1 encoding
  (the "E" character set is accessed via an option on "set encoding" )
  T S Z E       T S Z E      T S Z E      T S Z E      T S Z E
  ........      ........     ........     ........     ........

with columns consisting of special signs. But you will run into problems when you try using the last column signs 'E', since they need a line in gnuplot before use:

  gnuplot> set encoding iso_8859_1

So, if you are an astronomer and you want your x-axes to be a wavelength in angstroms, you should type in gnuplot:

  gnuplot> set encoding iso_8859_1
  gnuplot> set xlabel '{/Symbol l}({/E \305})' 

since angstrom = E \305 as you may find in the file mentioned above.

Further, it's only for Serbians, Croatians, Bosniacs ... in word ex-yu for it deals with their special letters


Znaci, pokusavamo da ubacimo nasa slova u gnuplot. Pa, da krenemo redom ... Treba prevariti gnuplot, odnosno reci mu da stavi neki karakter na slovo. PRVO je potrebno ukljuciti

  gnuplot> set term postscript eps enhanced 'Times-Roman' 30; set encoding iso_8859_1;

A onda postaviti proizvoljan output i plotovati nesto. Ako su nam slova potrebna u naslovu, stvar ce dalje izgledati ovako

  gnuplot> set output 'proba.eps'; plot 'proba.txt' u 1:2; set title '~Z{.7/*0.6v}eljo moja=
  Zvezdo moja';

A za mala slova samo promeniti velicine i staviti malo slovo

  gnuplot> set title 'Tu~z{.5/*0.45v}no je biti navija~c{.5/*0.45v} Partizana';

A sad malo objasnjenja ... komanda govori ~ stavi .5 iznad slova z - 45 % velicine fonta koji koristim - znak v. Potpuno je analogno za 'sh' i 'ch':

  gnuplot> set term postscript eps enhanced 'Times-Roman' 30; set encoding iso_8859_1;\
  set output 'proba.eps'; plot 'proba.txt' u 1:2; set title '~C{.7/*0.6v}ekam...Du~s{.5/*0.45v}ana';

E, sad dolazimo do \'c, i tu se samo malo brojevi menjaju do prilagodjavanja ...

  gnuplot> set title '~C{.75/*0.4\/}evap vs. pljeka'; set xlabel '~c{.55/*0.3\/}evap'; 
  set ylabel 'pljeka';

I konacno, slovo 'dj':

  gnuplot> set title 'Moj brat ~D{.12/*0.9\-  }ole ~d{.4/*0.6\ -}olence iz milo~s{.5/*0.45v}te';

Plot

Axes range

There are two ways of limiting the axes

1. This is automatically done if we don't specify the range. Since those values chosen automatically by gnuplot may be used for some purposes, it is convenient to know them. So, if we don't specify anything, x-range is limited from GPVAL_X_MIN to GPVAL_X_MAX, and y-range from GPVAL_Y_MIN to GPVAL_Y_MAX. We can print these numbers on the screen, like all the other variables, predefined or defined by user as:

  gnuplot>> print GPVAL_X_MIN, ":", GPVAL_X_MAX

For example, we can use this if we need to make two x-axes, both upper and lower but having different values. Say, we want to have both x on the lower and log10(x) on the upper axis. We can make it work in several steps:

  gnuplot>> plot 'file.txt'; set x2range[log10(GPVAL_X_MIN):log10(GPVAL_X_MIN)]; set x2tics border; replot

or,

 gnuplot>>plot 'file.txt'; set x2range[10**GPVAL_X_MIN:10**GPVAL_X_MAX]; set x2tics border; set grid x2tics;replot

if we already have the logarithm and need normal x-scale on the upper axis. Here, set x2tics border means DISPLAY the values on the graph. Otherwise, values are calculated, but not displayed. And additionally, grid is plotted using new numbers. One should be careful with these predetermined values, for the first graph could not be done if x < 0, and that is one good reason to control the limits. The other would be to have several data to compare at the end - it may be annoying if ranges are different, so we can

2. Define both x- and y-range, which can be done simply by typing

  gnuplot>>xmin=0.28; xmax=0.4; set xrange[xmin:xmax]
  gnuplot>>plot 'file.txt'; set x2range[10**xmin:10**xmax]; set x2tics border;  

for example. And likewise

  gnuplot>>ymin=0.075; ymax=0.11; set yrange[ymin:ymax]
Personal tools