Sunday, October 30, 2011

Add value labels to the top of bars in a bar chart

It is easy to plot a bar chart with gnuplot using plot style boxes or histogram. This time we talk about how to add values labels to the top of bars in a bar chart.

The first coming idea is adding labels manually using command "set label ...". This a method but a poor efficient one. A better one is plot the labels with plot style "labels". And this is the method we will talk about here. A sample script is shown below.
reset
set term png font "Times,18"    #set terminal and output file
set output "bar_labels.png"
set xlabel "x value"    #set x and y label
set ylabel "Frequency"
set xrange [-0.5:4.5]    #set x and y range
set yrange [0:4]
set xtics 0,1,4    #set xtics
set style fill solid    #set plot style
set boxwidth 0.5
unset key    #legend not plotted
plot "bar_data.dat" using 1:2 with boxes,\
     "bar_data.dat" using 1:2:2 with labels
#plot bar chart and the value labels on the bars
In this script, we have used a data file (bar_data.dat) like this one
0    2
1    3
2    3
3    2
4    2

Run the script, we get picture file bar_labels.png like the following one.

Gnuplot bar chart with value labels

If you do not like the position of the labels in the upper picture, you can just change it. For example,
lot "bar_data.dat" using 1:2 with boxes,\
     "bar_data.dat" using 1:($2+0.25):2 with labels
will put the labels 0.25 units higher.

Value labels with a 0.25 units higher position

7 comments:

  1. Hello, im new to gnuplot and have been following your post. I have this code, how do i add labels on top of the histogram like in this post ? can you explain a little about it ?


    reset

    set term png font "Times,12"
    set output "Recognition1.png"
    set xlabel "Speaker"
    set ylabel "Average Recognition Rate"
    set yrange [0:100]
    set xtic
    set grid
    set boxwidth 0.3 relative
    set style fill transparent solid 1.0 noborder
    plot 'C:\Users\tarmizi\Documents\gnuplot Document\WaveCeps.dat' using 2:xtic(1) with boxes lc rgb"blue" title "MFCC Recognition"

    ReplyDelete
    Replies
    1. plot 'C:\Users\tarmizi\Documents\gnuplot Document\WaveCeps.dat' using 2:xtic(1) with boxes lc rgb"blue" title,\
      plot 'C:\Users\tarmizi\Documents\gnuplot Document\WaveCeps.dat' using 2:xtic(1):xtic(1) with labels

      You can refer to section "plotstyle labels" in the official document for some explanation.

      Delete
    2. Probably should be:

      plot 'C:\Users\tarmizi\Documents\gnuplot Document\WaveCeps.dat' using 2:xtic(1) with boxes lc rgb"blue" title,\
      plot 'C:\Users\tarmizi\Documents\gnuplot Document\WaveCeps.dat' using 0:2:2 with labels

      xtic doesn't count as a data column, thus gnuplot will report that there is not enough data column for the "label" style plotting. Use "0" in place of the xtic.

      Delete
  2. Thank you very much for the reply... i'll try that :)

    ReplyDelete
  3. I am new to gnuplot. I have done couple of plots using the gnuplot. Your tutorial here has helped me a lot. But what do you mean by "Run the script, we get picture file bar_labels.png like the following one."
    I am having troubles

    ReplyDelete
  4. This was a really great contest and hopefully I can attend the next one. It was alot of fun and I really enjoyed myself.. labels

    ReplyDelete

Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.