Gilfoyle
Gilfoyle

Reputation: 3616

Gnuplot Bar chart with error bars

I have the following data

Name   Value of the bar   Confidence interval
A      0.62               [0.59 0.63]
B      0.64               [0.54 0.72]
C      0.51               [0.46 0.67]
D      0.33               [0.25 0.36]

I tried to plot it as a bar chart with A,B,C and D labeling each bar and with and error bar.

By using

plot "my.dat" using 1; with boxes I only get a bar chart. Can someone help me?

Upvotes: 3

Views: 4445

Answers (2)

theozh
theozh

Reputation: 25734

Just for the records and in addition to Christoph's solution, for those users who do not have sed available. There is a simple gnuplot-only solution without external tools, hence, platform-independent.

Standard column separator is whitespace. So, in order to handle the unfortunate brackets in the input data, take strcol(3) (check help stringcolumn), neglect the first character (check help substr) and convert it into a real number (check help real). For the 4th column gnuplot will ignore the trailing ] without further action.

Data: SO24871941.dat

Name   Value of the bar   Confidence interval
A      0.62               [0.59 0.63]
B      0.64               [0.54 0.72]
C      0.51               [0.46 0.67]
D      0.33               [0.25 0.36]

Script: (works for gnuplot>=4.4.0, March 2010)

### add errorbar to boxes from special data format
reset

FILE = "SO24871941.dat"

set style fill solid 0.3
set boxwidth 0.8
set yrange[0:*]
set key noautotitle

plot FILE u 0:2:0:xtic(1) w boxes lc var, \
       '' u 0:2:(real(strcol(3)[2:])):4 w yerr lc rgb "black" pt 7 lw 2
### end of script

Result: (created with gnuplot 5.2.8)

enter image description here

Upvotes: 2

Christoph
Christoph

Reputation: 48390

If you also want errorbars, you must add a second plot with the yerrorbars plotting style. The brackets aren't very handy in the data file, so I remove them with a sed command:

set style fill solid
set boxwidth 0.8
set yrange [0:*]
unset key
plot "< sed 's/[][]//g' my.dat" using 0:2:xtic(1) with boxes, \
     '' using 0:2:3:4 with yerrorbars lc rgb 'black' pt 1 lw 2

enter image description here

Upvotes: 4

Related Questions