con
con

Reputation: 6093

Add Error Bars to GNUPlot Barplot

I have a very simple chart like this I want to add error bars here

and I would like to add vertical error bars above and below the top of each bar.

my GNUPlot input is

set boxwidth 0.5
set style fill solid
set title 'title'
set key off
set ylabel 'ylabel'
set xtics font 'Arial, 24'
set ytics font 'Arial, 18'
set title font 'Arial, 24'
set ylabel font 'Arial, 16'
set terminal pngcairo size 1000,1000# gnuplot recommends setting terminal before output
set output 'pathfinder.png'
plot 'pathfinder.txt' every ::0::0 using 1:3:xtic(2) with boxes lc rgb 'red', \
    'pathfinder.txt' every ::1::1 using 1:3:xtic(2) with boxes lc rgb 'green', \
    'pathfinder.txt' every ::2::2 using 1:3:xtic(2) with boxes lc rgb 'blue', \
    'pathfinder.txt' every ::3::3 using 1:3:xtic(2) with boxes lc rgb 'orange',

and my data

#x axis location, label, height of bar, low part of error bar, high part of error bar
0 A 209.3 200 219
1 B 4790.2 4700 4900
2 C 3771.2 3700 3900
3 D 170.3 150 200

I have seen similar posts, namely adding error bar to histogram in gnuplot and Adding error bars on a bar graph in gnuplot but I don't see how it applies with this case.

How can I modify my GNUPlot script to add error bars?

thanks so much!

Upvotes: 0

Views: 344

Answers (1)

theozh
theozh

Reputation: 25684

The two links you mentioned basically contain the answer, however, from you example it looks like you want filled bars with defined colors.

From help boxerrorbars:

The error bar will be drawn in the same color as the border of the box.

So, you have several options:

  1. (top left) "same color as the border of the box" of course doesn't make sense if you fill your bar, since the lower part of the error bar will disappear in the filled bar.

  2. (top right) set the border e.g. "black", which will make the errorbar black as well

  3. (bottom left) set the intensity of the solid fill, e.g. to 0.3, which will leave the border in intensity 1.0 and hence the errorbar as well.

  4. (bottom right) split the plot in with boxes and with yerrorbars then your are completely independent for the colors. There is a point with the yerrorbar which you can let disappear by specifying ps 0 (or pointsize 0)

Script: (works with gnuplot>=5.0.0, Jan. 2015)

### filled bars with errorbars
reset session

$Data <<EOD
#x axis location, label, height of bar, low part of error bar, high part of error bar
 0  A   25   17   33
 1  B   50   40   60
 2  C   85   71   91
 3  D   40   33   47
EOD

set palette defined (1 "red", 2 "green", 3 "blue", 4 "orange")
unset colorbox
set key noautotitle
set yrange[0:100]
set grid y
set tics out
set offset 0.25,0.25,0,0

set multiplot layout 2,2

    set style fill solid 1.0 noborder
    plot $Data u 1:3:4:5:(0.5):0:xtic(2) w boxerrorbars lc palette

    set style fill solid 1.0 border lc "black"
    replot
    
    set style fill solid 0.3 border
    replot
    
    set style fill solid 1.0
    plot $Data u 1:3:(0.5):0:xtic(2) w boxes lc palette, \
            '' u 1:3:4:5 w yerr pt 6 ps 1 lc "black"

unset multiplot
### end of script

Result:

enter image description here

Upvotes: 1

Related Questions