Search code examples
gnuplot

Clustered bar plot in gnuplot with errorbars


I'm new to using gnuplot and I've followed this question which plots the data as I desire. However, I'd very much like to also include error bars. I've tried to do so by adding min and max error columns as follows:

Broswer,Video,min,max,Audio,min,max,Flash,min,max,HTML,min,max,JavaScript,min,max
IE,30%,5,5,10%,5,5,25%,5,5,20%,5,5,15%,5,5
Chrome,20%,5,5,5%,5,5,35%,5,5,30%,5,5,10%,5,5

Which I then try to plot with the script modified as follows:

set terminal pdf enhanced
set output 'bar.pdf'

set style data histogram
set style histogram cluster gap 1

set style fill solid border rgb "black"
set auto x
set yrange [0:*]
set datafile separator ","

plot 'data.dat' using 2:xtic(1) title col with yerrorbars, \
        '' using 3:xtic(1) title col with yerrorbars, \
        '' using 4:xtic(1) title col with yerrorbars, \
        '' using 5:xtic(1) title col with yerrorbars, \
        '' using 6:xtic(1) title col with yerrorbars

From what I understand from reading this should also plot errorbars, but I get the error:

"plot2", line 16: Not enough columns for this style

Googling this error informs me that it has something to do with the first column being non-numerical. I've tried a few suggestions including this one, but nothing has worked so far. So, any suggestions? Thanks.


Solution

  • This error tells you, that the yerrorbars plotting style requires more than one column for plotting (the xtic(1) takes a special parts). Looking at the documentation, you can see, that you can use either two, three or four columns. I don't go more into detail, because the with yerrorbars selects a completely new plotting style and you don't get any histogram at all.

    In order to plot clustered histograms, you must add errorbars to the histogram's style definition, and of course you must give the column for the yerror values:

    set style data histogram
    set style histogram cluster gap 1 errorbars
    
    set style fill solid border rgb "black"
    set auto x
    set yrange [0:*]
    set datafile separator ","
    
    plot 'data.dat' using 2:3:xtic(1) title col(2),\
            '' using 5:6 title col(5), \
            '' using 8:9 title col(8), \
            '' using 11:12 title col(11), \
            '' using 14:15 title col(14)
    

    Or, in shorter notation

    plot for [i=2:14:3] 'data.dat' using i:i+1:xtic(1) title col(i)
    

    enter image description here

    If you explicitly need to plot min and max values, than you must add a third column. But then the last two columns are ymin and ymax and not delta values. Judging from you data file error, the values in the data file are deltas, so the plot command should be:

    plot for [i=2:14:3] 'data.dat' using i:(column(i) - column(i+1)):(column(i) + column(i+2)):xtic(1) title col(i)