Gnuplot

From MediaWiki

Revision as of 22:47, 19 January 2008 by Ana (Talk | contribs)
Jump to: navigation, search

Very short manual for the beginners:

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 'Moj tata ~Z{.7/*0.6v}eljko';

A za mala slova samo promeniti velicine i staviti malo slovo

  gnuplot> set title 'moj ~z{.5/*0.45v}eljko';

A sad malo objasnjenja ... komanda govori ~ stavi .5 iznad slova z i to .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...sme~s{.5/*0.45v}no';

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

gnuplot> set title 'moj ~c{.55/*0.3\/}evap';plot 'proba.txt' u 1:2 title '~C{.75/*0.4\/}evap';

I konacno, slovo 'dj':

gnuplot> set title 'moj brat ~d{.4/*0.6\-}ole';plot 'proba.txt' u 1:2 title '~D{.12/*0.9\- }ole'

Plot

Personal tools