Search code examples
plotgraphgnuplot

Line plot in GnuPlot where line width is a third column in my data file?


I have a datafile that has three columns :

1 1.0 1
2 1.5 2
3 0.0 3
4 1.2 2.5
5 1.0 1
6 1.1 5

where the first column is my X value, the second column is my Y value, and the third column is the line width. I'd like for each line segment to be plotted according to the third column line width.

I tried:

plot 'file1.dat' using 1:2:3  with lines lw var

But I get undefined variable: var error.

Is this possible in gnuplot?

Thanks.


Solution

  • If you define column 3 as the linewidth between points n and n+1 (so the value of col. 3 of the row will be ignored) you can cheat:

    stat 'file1.dat'
    n=STATS_records
    plot for [i=0:0] 'file1.dat' using 1:2:(alma=$3) every ::i::i w l lc 1 lw 1
    plot for [i=0:n-1] 'file1.dat' using 1:2:(alma=$3) every ::i::i+1 w l lc 1 lw alma notitle
    

    OR

    plot 'file1.dat' u 0:1
    n=GPVAL_DATA_X_MAX
    plot for [i=0:0] 'file1.dat' using 1:2:(alma=$3) every ::i::i w l lc 1 lw 1
    plot for [i=0:n] 'file1.dat' using 1:2:(alma=$3) every ::i::i+1 w l lc 1 lw alma notitle
    

    enter image description here

    You need the first plot for[i=0:0] to 'initialize' variable 'alma'.