Search code examples
gnuplothistogramerrorbar

Show error bars in a multiaxis plot in Gnuplot


I have a dataset (show-errorbar.dat) containing:

Model# DE IE Error

Apple -4.6 -128.9538 4.0

Huawei -5.2 -176.6343 5.3

One-Pro -5.2 -118.1106 3.2

#!/usr/bin/gnuplot
#set terminal pdfcairo enhanced color font 'Helvetica,12' linewidth 0.8
set terminal png
set output 'BrandError.png'

set boxwidth 1.0 relative
set bmargin 5
set style fill solid border -1
set xtic rotate by -45 scale 0
#set auto x

set style line 81 lt 0 lc rgb "#808080" lw 0.5

set grid xtics
set grid ytics
set grid mxtics
set grid mytics
set grid back ls 81

set arrow from graph 0,first -4.6 to graph 1, first -4.6 nohead lw 2 lc rgb "#000000" front

set border 11
set border lw 2.0

set xtics font ",11"
set ytics font ",14"
set tics out
set ytics nomirror
set y2tics
set y2tics font ",14"


set mxtics 10
set mytics 2
set my2tics 2

set yrange [-10:0]
set y2range [-260:0]

set key left bottom

set y2label offset -2
set ylabel offset 2

set ylabel 'DE' tc rgb "red"
set y2label 'IE' tc rgb "green"

set style data histograms
set style histogram cluster gap 2

set linetype 2 lc rgb 'red'
set linetype 3 lc rgb 'yellow'
set linetype 4 lc rgb 'green'


plot 'show-errorbars.dat' using 2 ti 'DE' lc 2 axis x1y1, '' u 3:xticlabels(1) ti 'IE' lc 4 axis x1y2
set output

enter image description here I would like to plot a histogram comparing DE vs IE and also show error bars (data in column 4) for the IE values.

Please any help on how to go about it.


Solution

  • There is a variant histogram style for exactly that purpose set style histogram errorbars gap 2 {lw W}. Here is the help section from the docs:

    The `errorbars` style is very similar to the `clustered` style, except that it
    requires additional columns of input for each entry. The first column holds
    the height (y value) of that box, exactly as for the `clustered` style.
          2 columns:        y yerr          bar extends from y-yerr to y+err
          3 columns:        y ymin ymax     bar extends from ymin to ymax
    The appearance of the error bars is controlled by the current value of
    `set errorbars` and by the optional <linewidth> specification.
    

    Updated answer

    Notes:

    1. You can't mix axis choice within a single histogram. So I have removed the axes x1y1 and axes x1y2 from the plot command. Since you have explicitly given the range for both y1 and y2, the plot border and labels are not affected.

    2. However since the green bars are now being plotted against y1, we have to scale them so that the y2 axis labels apply. So the column 3 and column 4 values will be divided by 26, which is (y2 range) / (y1 range)

    3. In "histogram errorbars" mode each plot component looks for an extra column of data to determine the size of the errorbar. Since your column 2 data has no corresponding column of errors, we dummy it up to use all a constant not-a-number (no data) value: (NaN)

    4. Your data contains a line of columnheaders, which could confuse the program if it thinks this is a line of data. There are a number of ways you can tell the program to skip this line; I have used set key autotitle columnhead for convenience and because it is supported by old versions of gnuplot. If you have a current version it would be better to use instead set datafile columnheaders.

    I have kept all of your commands except that the plot command is replaced by the following 3 lines:

    set style histogram errorbars gap 2 lw 1.5
    set key autotitle columnhead
    
    plot 'show-errorbars.dat' using 2:(NaN) ti 'DE' lc 2, '' u ($3/26.):($4/26.):xticlabels(1) ti 'IE' lc 4
    

    enter image description here