Search code examples
gnuplot

Plot a error bar as shaded region in GNUPLOT


I have plotted a graph (X-top axis, Y-bottom axis) with fsteps function in Gnuplot. Next, I tried to add an error bar as a shaded region(transparent) to the graph, but unable to plot it on the graph. Below is the code so far I have tried and also attached the graph.

#!/usr/bin/gnuplot
reset
set border lw 30
set term pngcairo size 10000,10000 font "arial-Bold,130"
set output 'out.png'
unset key
set size ratio 1.2
set style data lines
set xtics format ""
set x2tics nomirror
set ytics out nomirror
set ytics 0,20 
set x2label "Vs (km/s)" offset -1.0
set ylabel 'Depth (km)' offset 1.5
set xrange [2.5:4.8]
set yrange [314:0]
set label 3 at 2,120
set key samplen 1.7 at 3.0,135
#
set label 1 '(a)' font "arial-Bold,130" at 0.8,15 right 
set label 3 "C3 (MNAI)" center font "arial-Bold,130"
set style fill transparent solid 0.25
set style fill noborder

plot 'MAN.inmd' lc rgb 'blue' lw 35 title "Initial  model"   with fsteps,\
     'MAN.outmd' using 1:2 lc rgb 'red' lw 35  dt"-" title "Inverted model" with fsteps ,\
     'MAN.outmd' using 1:($2-$3):($2+$3) with filledcurve lc "blue" notitle, 

Example Data for file MAN.outmd X Y Z(Error)

0        3         0
0.4475   3.1       0
0.4475   3.5       0
2.6738   3.6       0.0552
2.6738   5         0.0552
3.8441   5.1       0.0592
3.8441   8         0.0592
3.6302   8.1       0.0395
3.6302   15.935    0.0395
4.5176   15.1      0.041
4.5176   113.296   0.041
4.2443   113.3     0.1024
4.2443   214       0.1024
4.4584   214.1     0.1077
4.4584   314       0.1077

enter image description here

I want output should be as given below (example)

enter image description here


Solution

  • gnuplot can easily fill the area between two "horizontal" curves (i.e. unique x-values), but as far as I know, not between two vertical curves. However, gnuplot can fill some enclosed areas. So, the workaround is to create datapoints which surround the area to be shaded. For this, you "plot" the data into a datablock, once "forward" with x-dx and once "backwards" with x+dx. This can be done easiest if you have the data already in a datablock, because then you can easily loop the data forward and backwards. In case you have your data in a file, see here: gnuplot: load datafile 1:1 into datablock

    Code:

    ### fill between vertical curves
    reset session
    
    $Data <<EOD
    0        3         0
    0.4475   3.1       0
    0.4475   3.5       0
    2.6738   3.6       0.0552
    2.6738   5         0.0552
    3.8441   5.1       0.0592
    3.8441   8         0.0592
    3.6302   8.1       0.0395
    3.6302   15.935    0.0395
    4.5176   15.1      0.041
    4.5176   113.296   0.041
    4.2443   113.3     0.1024
    4.2443   214       0.1024
    4.4584   214.1     0.1077
    4.4584   314       0.1077
    EOD
    
    # create datablock with circumference of shaded area
    set print $XErrorFill
        do for [i=1:|$Data|] {
            print real(word($Data[i],1))-real(word($Data[i],3)), real(word($Data[i],2))
        }
        do for [i=|$Data|:1:-1] {
            print real(word($Data[i],1))+real(word($Data[i],3)), real(word($Data[i],2))
        }
    set print
    
    set yrange [:] reverse
    set style fill noborder
    
    plot $XErrorFill u 1:2 w filledcurves lc "light-grey" notitle, \
         $Data u 1:2 w l lw 1.5 lc rgb "red" notitle
    ### end of code
    

    Result:

    enter image description here