2
votes

I have a datafile with some points that looks like this (notice that some values are missing):

x   A   1-A
0   1   0
0.25    0   1
0.5
0.75    0   1
1   1   0
1.25    0   1
1.5
1.75    0   1
2   1   0
2.25    0   1
2.5
2.75    0   1
3   1   0
3.25    0   1
3.5
3.75    0   1
4   1   0
4.25    0   1
5

I would like to plot this data into a graph that looks like this (notice that the pink line is the max of the other two lines at all times):

enter image description here

In order to do so, I have the following gnuplot code, which works well for all but the pink line:

gnuplot> max(x,y) = (x>y) ? x : y
gnuplot> plot "dataset1" using 1:2 title "A" with lines lc rgbcolor "black" lw 4, \
>"dataset1" using 1:3 title "1-A" with lines lc rgbcolor "blue" lw 4, \
>"dataset1" using 1:(max($2,$3)) title "Fuzzy(A)" with lines lc rgbcolor "purple" lw 4

However, this produces the following graph (notice that the purple line does not do what the pink line in the previous image does):

enter image description here

How could I go about producing a graph that looks like the first image, as opposed to what I have?

1

1 Answers

0
votes

The are two reasons for this:

  1. As long as you don't do calculations inside the using statement, the empty 'fields' are treated as missing data. If you have two points with a 'missing' point between them, they are still connected with a line. If the point between is undefined (as it becomes when you calculate), the other two points aren't connected. The simplest case showing this is:

    set yrange[-0.1:1.1]
    set multiplot layout 2,1
    plot 'dataset1' using 1:2 with lines
    plot 'dataset1' using 1:($2) with lines
    unset multiplot
    

    So you must filter you data with an external tool in order to plot it correctly (see also In gnuplot, with “set datafile missing”, how to ignore both “nan” and “-nan”?):

  2. You need to calculate the intersections between the two curves in order to get your 'purple' line.

Here is a variant using awk which does both the filtering (uses only the rows which have three fields (if (NF == 3) and skips the first line which the description), and calculates the intersections and add them to the output:

max(x,y) = (x>y) ? x : y
plot "dataset1" using 1:2 title "A" with lines lc rgbcolor "black" lt -1 lw 4, \
     "dataset1" using 1:3 title "1-A" with lines lc rgbcolor "blue" lt -1 lw 4, \
     "< awk 'BEGIN { prevX = prevA = prevN = currX = currA = currN = -1 } \
        { if (NF == 3 && NR > 1) { \
            if (currA != -1) { prevA = currA; prevN = currN; prevX = currX } \
            currX = $1; currA = $2; currN = $3; \
            if ((prevA != -1) && (prevA != currA)) { \
               print 0.5*(currX + prevX), 0.5*(currA+prevA), 0.5*(currN+prevN); \
            }\
            print \
          }\
        }' dataset1" \
      using 1:(max($2,$3)) title "Fuzzy(A)" with lines lc rgbcolor "purple" lt 2 lw 4

With some other little settings

set termoption dashed
set key above right
set autoscale fix
set offset 0,0,0.1,0

and 4.6.4 I get the following result:

enter image description here