Search code examples
gnuplothistogram

Adding labels on top of bar using Gnuplot


I am trying to add values on top of the bars of the histogram. I realised that a lot of answers are available, and I tried a few such as following this one gnuplot histogram: How to put values on top of bars. however, the result does not have any values. Could someone guide me reading this.

clear
reset
unset key

$Data <<EOD
Case Case1 Case2 Case3
Value1  33.22   71.62   76.11
Value2  29.82   34.08   28.41
EOD
set style data histogram
set style fill solid border
set style histogram clustered
plot for [COL=2:4] $Data with boxes fill pattern 1, '' u 1:($2 + 0.5):($2) with labels
set key
set yrange [0:100]
set term png
set output "histogram_demo.png"
replot

enter image description here


Solution

  • There are several problems here

    • you need to tell the program that the first line of $Data is text columnheaders
    • you can't tell it the plot style is histograms and then plot instead with boxes
    • the iteration variable COL is never used; it should be in the using specifier
    • you will have to adjust the x position of the labels to match the histogram layout.

    For example

    $Data <<EOD
    Case Case1 Case2 Case3
    Value1  33.22   71.62   76.11
    Value2  29.82   34.08   28.41
    EOD
    set key autotitle columnhead
    set style data histogram
    set style fill solid border
    set style histogram clustered
    plot for [COL=2:4] $Data using COL fill pattern 1, \
         for [COL=2:4] '' u ($0 + COL*0.2 - 0.6):COL:COL with labels offset 0,1
    

    enter image description here