3

I have a data file with three columns: Xi, Yi, and Zi like this:

1  1    2
2  4    4
3  9    6
4  16   8
5  25   10

I need to plot Yi and Zi against Xi. So I use the following command:

plot 'speed.txt' using 1:2 with lines, 'speed.txt' using 1:3 with lines

and get this plot:

enter image description here

But the problem is that my data file is not in three columns most of the times. Basically I have two different data types which are stored in two columns. So, the same three-column file above will be like this in two column format: (the last column shows the car that has generated this data).

1  1    car1
1  2    car2
2  4    car1
2  4    car2
3  9    car1
3  6    car2
4  16   car1
4   8   car2
5  25   car1
5  10   car2

There is no special pattern in the data types, meaning car1 can generate 10 rows then car3 generates 2 rows, etc. and they are all mixed (something like a log file that events happen asynchronously).

Is there any way that I can get the same plot out of these data? For instance use the name of the car as a key to separate different data types.

0

1 Answer 1

1

Here's one way you could do it. It doesn't scale very well for more than two different names in the second column but it does work:

set datafile separator ","
plot '<awk ''{printf "%s,%s\n", $1, $3=="car1"?"," $2:$2}'' cars' u 1:2, '' u 1:3

This puts the "car1" data into column two and the "car2" data into column three.


If scalability is what you're after, this will work for many different numbers of columns. It uses the same approach but dynamically adds new output columns for every unique name in the third column:

plot '<awk '' \
function r(n) { s=""; for(j=0;j<n;++j) s=s ","; return s } \
{ a[$3,$1] = $2 } \
!seen[$3] { seen[$3] = ++c } \
END{ \
    for (i in a) { \
        split(i,b,SUBSEP); \
        printf "%s%s%s\n", b[2], r(seen[b[1]]),a[i] \
    } \
}'' cars' using 1:2, '' using 1:3

As much as I like using awk with gnuplot, I think this script is bordering on the long side...in order to increase readability, you might want to make it into a separate awk script:

cars.awk

# repeat comma n times
function r(n) { 
    s=""
    for (j = 0; j < n; ++j) 
    s = s ","
    return s 
}

# add each element to array, indexed on third,first column
{ a[$3,$1] = $2 }

# register any new names seen in column three
!seen[$3] { 
    seen[$3] = ++c
}

END {
    for (i in a) {
        # the index $3,$1 from above is separated by 
        # the built-in variable SUBSEP                  
        split(i, b, SUBSEP)
        # now b[1] is the name (car1 or car2)
        # which is used to determine how many commas
        # b[2] is the x value
        # a[i] is the y value
        printf "%s%s%s\n", b[2], r(seen[b[1]]), a[i] 
    }                   
}

Then in your gnuplot, just call the script:

plot '<awk -f cars.awk cars' using 1:2, '' using 1:3

The result (using either approach):

plotted data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.