Gnuplotting

Create scientific plots using gnuplot

April 2nd, 2012 | 6 Comments

Since last month the new Gnuplot version 4.6 is officially available. There are a lot of interesting changes in this new version and we will cover the bigger ones within the next posts. Here we start with, in my opinion, the nicest new feature: block-structured conditions and loops.

Until 4.6 an iteration over different lines of code was only possible with the help of an extra file. This technique was used, for example, for the gif animation entry. There the loop was executed by

t = 0
end_time = 1
load 'bessel.plt'

with the file bessel.plt containing the code to execute within the loop

# bessel.plt
t = t + 0.02
outfile = sprintf('animation/bessel%03.0f.png',50*t)
set output outfile
splot u*sin(v),u*cos(v),bessel(u,t) w pm3d ls 1
if(t<end_time) reread;

This can now be reformulated in a much shorter way by applying the new do command and the block-structure given by the { }

do for [t=0:50] {
    outfile = sprintf('animation/bessel%03.0f.png',t)
    set output outfile
    splot u*sin(v),u*cos(v),bessel(u,t/50.0) w pm3d ls 1
}

Now there is no need for an additional file. The only thing to consider is the change of the index t, because for the for-loop t has to be an integer.

The block-structure can in the same way be applied to the if-statement.

6 Comments

  1. DarioP says:

    I would like to do this in parallel :)

  2. keghn says:

    Hello.

    I do these lines in a script:

    y(x) = 0.5 * m1 * x**2 + 0.5 * m2 * v3(x)**2 + 0.5 * i * w(x)**2 – 0.5 * m1 * v1**2
    if( y(x) == 0) then gg = 4

    and get:

    gnuplot> load ‘0s.p’
    “0s.p”, line 57: undefined variable: x
    the error is on the “if” line:

    Is their a way to to this in gnuplot.

    Thank for any help,

    keghn

  3. hagen says:

    There is no explicit x-value in your second line. Maybe a second function is what you are looking for:

    y(x) = 0.5*m1*x**2 + 0.5*m2*v3(x)**2 + 0.5*i* w(x)**2 - 0.5*m1*v1**2
    gg(x) = y(x)==0 ? 4 : 0
    
  4. keghn says:

    ah……. no.
    i want to take the 4 and put it into another equation. when y(x) = 0 and I
    want gg(x) to stay the value of four and not to switch back to 0, or 0/1.
    Kind of like capture a value.

    Even better would be this one:

    if(y(x)== 0) gg(x) = v1(x)

    But it seems the “if” functions do not work with functions as will as
    this:

    gg = f(x)

    thanks for your help.
    Cheers.
    keghn

  5. Sergio says:

    Hi!

    I have the 400 files containing x,y data named as data001, data002, …, data400. I want to make a curve fitting in each file and plot the value of the parameters as a function of the file index.

    Could you help me with that?

    Thank you

  6. hagen says:

    Hi Sergio.

    I haven’t tested it, but I should be possible in a similar way I did it in the U.S. states post. You can store the values of your parameters in a string variable and then indexing the string. I don’t know if it is the best method to do it, but it should work. Here is an code example (which I have not tested):

    f(x) = a*x+b
    A = []
    B = []
    do for [n=1:400] {
       file = sprintf('data%03.0f',n)
       fit f(x) file via a,b
       A = A.sprintf(%f,a)
       B = B.sprintf(%f,b)
    }
    plot for [n=1:400] '+' u (n):(A[n:n]), for [n=1:400] '+' u (n):(B[n:n]) 
    

Leave a Reply