2

I am referring this example to draw a bar chart. I like, the exact value to be displayed on top of each of the red bars.

Like, those numeric values that are circled in green,

enter image description here

My gnuplot code is,

set size 1, 1
set term png size 600, 400
set title "sk plot"
set output "figure.png"
set boxwidth 0.75
set style fill solid
set title "Population of Australian cities (millions), as of June 2012"
plot "population.dat"  using 2:xtic(1) with boxes

Population.dat contains,

Adelaide    1.277174
Brisbane    2.189878
Canberra    0.374658
Darwin      0.131678
Hobart      0.216959
Melbourne   4.246345
Sydney      4.667283

1 Answer 1

3

You can do this by adding a second plot of the same data (shown by the filename ""), using with labels to add text at the given x,y co-ordinate calculated from column 0, i.e. just the data index, and column 2 with an offset so the text sits above the box, ($2+.1).

plot "population.dat"  using 2:xtic(1) with boxes,\
  ""  using 0:($2+.1):(sprintf("%3.2f",$2)) with labels notitle

The sprintf reduces the text printed to just 2 decimal places.

You can move the key shown at the top of the plot to stop it interfering using set key top left, for example.

enter image description here

2
  • When I add the labels then the barchart shifts towards right a little. Do you know why is this happening?
    – Sufyan
    Commented Dec 1, 2020 at 11:09
  • @Sufyan No I don't know why you are seeing this. Perhaps the labels end up wider than the bars and cause a rescaling. You could trying reducing the number of digits in the label, or the size of the font to see if it is the cause.
    – meuh
    Commented Dec 1, 2020 at 12:49

You must log in to answer this question.