1

I have file “data.txt” containing three columns. Column 1 is for x axis. I want to draw smooth curves corresponding to data points of column 2 and 3 and then want to fill colour between these two lines.

File content is;

10    -1.3     1.1
20    -0.956   0.933
50    -0.761   0.684
80    -0.523   0.439
110   -0.227   0.20
130   -0.07    0.06

My script lines are,

plot “data.txt” u 1:2 smooth bezier w filledcurves above,\
“data.txt” u 1:3 smooth bezier w filledcurves below

But I’m not getting desired shaded plot.

maij
  • 4,094
  • 2
  • 12
  • 28

3 Answers3

1

I guess you have two challenges here:

  • you cannot smooth and fill at the same time
  • you can only fill between two columns of the same dataset or file

One possible way would be the following:

  • plot the data smooth bezier into a table $Smooth
  • N is the number of lines the combined smoothed data $Smooth
  • merge these smoothed data by using line 1 and 1+N/2, line 2 and 2+N/2, etc. into a dataset $Paste
  • for the fill you then have to use columns 1, 2 and 5

Script:

### fill area between smoothed curves
reset session

$Data <<EOD
  0    0   90
 10   10   50
 50   20   40
 80   50   60
100   30   50
EOD

set table $Smooth
    set samples 20
    plot $Data u 1:2 smooth bezier
    plot $Data u 1:3 smooth bezier
unset table

set print $Paste
    N = |$Smooth|
    do for [i=1:N/2] {
        print $Smooth[i].$Smooth[i+N/2]
    }
set print

plot $Data u 1:2:3  w filledcurves  lc rgb 0xcc0000ff ti "fill between data", \
        '' u 1:2    w lp pt 7       lc "red"          ti "original data", \
        '' u 1:3    w lp pt 7       lc "red"          notitle, \
     $Smooth u 1:2  w lp pt 7       lc "green"        ti "smoothed curves", \
     $Paste  u 1:2:5 w filledcurves lc rgb 0xccff0000 ti "fill between smoothed"
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
1

For future reference, the development version of gnuplot (5.5) greatly extends the options for smoothing. For your case the obvious command works as expected. The only caveat is that for open curves (endpoints are not equal) you must use smooth sbezier rather than smooth bezier.

$DATA << EOD
10    -1.3     1.1
20    -0.956   0.933
50    -0.761   0.684
80    -0.523   0.439
110   -0.227   0.20
130   -0.07    0.06
EOD

set xrange noextend
set style fill transparent solid 0.25
plot $DATA using 1:2:3 smooth sbezier with filledcurves between, \
     '' using 1:2 with lp, '' using 1:3 with lp

enter image description here

Ethan
  • 13,715
  • 2
  • 12
  • 21
  • Sorry, I'm getting this error; `"plot.gnu" line 45: warning: extra columns ignored by smoothing option` – Pramod Sharma Mar 29 '22 at 08:03
  • `unexpected or unrecognized token` error is also coming for command filledcurves `between`. – Pramod Sharma Mar 29 '22 at 12:01
  • This feature was added by commits a6069e0a 89ab43ce 2cf73996. It should be usable in any snapshot of the development version newer than 04-Mar-2020. – Ethan Mar 29 '22 at 16:49
0

As far as I understand, above and below expect three column input, and this does not work with smooth.

But starting from this and this answer, you can try something like the following:

set xzeroaxis
set tics front

plot "data.txt" u 1:3 smooth bezier notitle w filledcurves x1 lc rgb "#b0ffff00", \
     "data.txt" u 1:2 smooth bezier notitle w filledcurves x1 lc rgb "#00ffffff", \
     "data.txt" u 1:3 smooth bezier lt 1, \
     "data.txt" u 1:2 smooth bezier lt 2

Both filledcurves plots reach from the curve to the x-axis, the second one in white covers the first one in yellow leaving the space between the two curves in yellow.

See help lc or help linecolor for color details.

This is the result: result

maij
  • 4,094
  • 2
  • 12
  • 28