0
votes

I want to plot a graph with gnuplot and the idea is that I will have a dataset, which I will plot form left to right and after that, plot the same data multiplied by 1.3 or something from right to left and once more plot the original data multiplied by 0.7 again from left to right.

This is my working code for the first plot from left to right, but I have no idea how to make it plot the remaning two. Variable DATA is the data file.

LINES=$(wc -l <"$DATA")
YRANGE=$(sort -n "$DATA" | sed -n '1p;$p' | paste -d: -s)

FMT=$TMPDIR/%0${#LINES}d.png

for ((i=1;i<=LINES;i++))
do
    {
        cat <<-PLOT
            set terminal png
            set output "$(printf "$FMT" $i)"
            plot [0:$LINES][$YRANGE] '-' with lines t ''
            PLOT
        head -n $i "$DATA"
    } | gnuplot
done

Can you please give me some hints ? Thank you very much

1

1 Answers

0
votes

The code below only represents the outline of my suggested approach; I'll leave it up to you to fill in the missing blanks 😉

I suggest to create a function mkplot which will be called 3 times with the appropriate parameters for multiplication factor, plot direction (1 = left to right; -1 = right to left), and input filename (just in case you need to reuse the plot routine for different files later on). awk is being used to do the multiplications, as well as the data output in normal and reverse orders. You'll need to change the print statements in the awk section to print your desired gnuplot headers.

DATA=data

function mkplot {
    local factor=$1
    local dir=$2
    local file=$3

    awk '
        # multiply each data point by factor
        { a[i++]= ($0 * '$factor') }
        END{
            # after all lines have been processed, output result
            print "set terminal png"
            print "add other gnuplot options here"
            print "..."
            print "plot \"-\""
            # dump lines in reverse order if dir=-1
            if ('$dir' == -1) {for (j=i-1;j>=0;j--) print a[j] }
            # or in standard order if dir=1
            else { for (j=0;j < i;j++) print a[j] }
        }
    ' $file | gnuplot    # pipe the awk output to gnuplot
}

# Call the plot routine for 3 plots with different factors and directions
mkplot 1 1 $DATA
mkplot 1.3 -1 $DATA
mkplot 0.7 1 $DATA