Home > Software engineering >  Gnuplot: How to display actual values on top of each bar in a bar-plot?
Gnuplot: How to display actual values on top of each bar in a bar-plot?

Time:02-23

First-timer with gnuplot. Essentially I'm interested in what the subject says. I already have a mechanism that works in terms of rendering the bars and I just want to add label-values on the top of each bar:

Plot

I have the following data in a file called 'data.dat':

1,Json,457054
2,MessagePack,685440
3,Cbor,1273723

I employ the following gnuplot configuration file 'plot.gp':

##
# file_path - path to the file from which the data will be read
# graphic_file_name - the graphic file name to be saved 
# y_label - the desired label for y axis
# y_range_min - minimum range for values in y axis
# y_range_max - maximum range for values in y axis
# column_1 - the first column to be used in plot command
# column_2 - the second column to be used in plot command
##

# graphic will be saved as 800x600 png image file
set terminal png

# allows grid lines to be drawn on the plot
set grid

# setting the graphic file name to be saved
set output graphic_file_name

# the graphic's main title
set title "Comparison"

# since the input file is a CSV file, we need to tell gnuplot that data fields are separated by comma
set datafile separator ","

# disable key box
set key off

# label for y axis
set ylabel y_label

# range for values in y axis
set yrange[y_range_min:y_range_max]

# to avoid displaying large numbers in exponential format
set format y "%.0f"

# vertical label for x values 
set xtics rotate

# set boxplots
set style fill solid
set boxwidth 0.5

# plot graphic for each line of input file
plot for [i=0:*] file_path every ::i::i using column_1:column_2:xtic(2) with boxes

And I run the following gnuplot command:

gnuplot                                                           \
            -e "file_path='data.dat'           "                  \
            -e "graphic_file_name='output.png' "                  \
            -e "y_label='y'                    "                  \
            -e "y_range_min='0000000''         "                  \
            -e "y_range_max='1500000'          "                  \
            -e "column_1=1                     "                  \
            -e "column_2=3                     "                  \
            plot.gp

I can't figure out how to use 'with labels' at the bottom of the .gp file. Any help appreciated.

CodePudding user response:

Here is yet another example. There is no need to do it in a for loop. You can use the pseudocolumn 0 (check help pseudocolumns) and lc var (check help linecolor variable) for setting the color.

Code:

### plot with boxes and labels
reset session

$Data <<EOD
1,Json,457054
2,MessagePack,685440
3,Cbor,1273723
EOD

set datafile separator comma
set style fill solid 0.3
set key noautotitle

set xrange[0.5:3.5]
set yrange[0:]
set format y "%.0f"
set grid x,y
set boxwidth 0.8 relative

plot $Data u 1:3:($0 1):xtic(2) w boxes lc var, \
        '' u 1:3:3 w labels offset 0,0.7
### end of code

Result:

enter image description here

  • Related