Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GNUPLOT - Two columns histogram with values on top of bars

yesteraday I made a similar question (this one). I could not display the value on top of bar in a gnuplot histogram. I lost many time because I couldn't find really good documentation about it, and I only can find similar issues on differents websites.

I lost many time with that but fortunately someone give me the solution. Now I am having a similar issue with an histogram with two bars, in which I have to put on top of both bars its value. I am quite near, or that is what I think, but I can't make it work properly. I am changing the script and regenerating the graph many times but I am not sure of what I am doing.

script.sh

#!/usr/bin/gnuplot
set term postscript
set terminal pngcairo nocrop enhanced size 600,400 font "Siemens Sans,8"
set termoption dash
set output salida
set boxwidth 0.8 absolute
set border 1
set style fill solid 1.00 border lt -1
set key off
set style histogram clustered gap 1 title textcolor lt -1
set datafile missing '-'
set style data histograms
set xtics border in scale 0,0 nomirror autojustify
set xtics  norangelimit
set xtics ()
unset ytics
set title titulo font 'Siemens Sans-Bold,20'
set yrange [0.0000 : limite1] noreverse nowriteback
set y2range [0.0000 : limite2] noreverse nowriteback
show style line


set style line 1 lt 1 lc rgb color1 lw 1
set style line 2 lt 1 lc rgb color2 lw 1

## Last datafile plotted: "immigration.dat"
plot fuente using 2:xtic(1) ls 1 ti col axis x1y1, '' u 3 ls 2 ti col axis x1y2, '' u 0:2:2 with labels offset -3,1 , '' u 0:2:3 with labels offset 3,1

I am modifying the last code line, because is here where I set the labels. I have been able to show both labels, but in bad positions, I have also been able to show one of the labels in the right position but no the other. I have been able to show almost everything but the thing that I want. This is the graph that generates the script.

output.png

enter image description here

This is the source file that I use for generating the graph

source.dat

"Momento" "Torre 1" "Torre 2" 
"May-16" 1500.8 787.8
"Jun-16" 1462.3 764.1
"Jul-16" 1311.2 615.4
"Ago-16" 1199.0 562.0
"Sep-16" 1480.0 713.8
"Oct-16" 1435.1 707.8

And that's the command that I execute with the parameters set

gnuplot -e "titulo='Energía consumida por torre (MWh)'; salida='output.png'; fuente='source.dat'; color1='#FF420E'; color2='#3465A4'; limite1='1800.96'; limite2='945.36'" script.sh

I think that is quite obvious what I am pretending, can someone help me?

Lots of thanks in advance.

like image 982
Luis González Avatar asked Nov 08 '16 10:11

Luis González


3 Answers

Your script has several problems, the missing ti col is only one of them. (You can also use set key auto columnheader, then you must not give that option every time).

  • Don't use both y1 and y2 axis if you want to compare the values! Otherwise the correct bar heights are only a matter of luck...

  • Understand, how gnuplot positions the histogram bars, then you can exactly locate the top center of each bar. If you only use offset with char values (which is the case when you give only numbers), then your script will break as soon as you add or remove a data row.

The histogram clusters start at x-position 0, and are positioned centered at integer x values. Since you have two bars in each cluster and a gap of 1, the center of the first bar is at ($0 - 1/6.0) (= 1/(2 * (numberOfTorres + gapCount))), the second one at ($0 + 1/6.0):

set terminal pngcairo nocrop enhanced size 600,400 font ",8"
set output 'output.png'
set title 'Energía consumida por torre (MWh)' font ",20"
set boxwidth 0.8 absolute
set border 1
set style fill solid 1.00 border lt -1
set style histogram clustered gap 1 title textcolor lt -1
set style data histograms
set xtics border scale 1,0 nomirror autojustify norangelimit
unset ytics

set key off auto columnheader
set yrange [0:*]
set offset 0,0,graph 0.05,0

set linetype 1 lc rgb '#FF420E'
set linetype 2 lc rgb '#3465A4'
# dx = 1/(2 * (numberOfTorres + gap))
dx = 1/6.0

plot 'source.dat' using 2:xtic(1),\
     '' u 3,\
     '' u ($0 - dx):2:2 with labels,\
     '' u ($0 + dx):3:3 with labels

enter image description here

Now, starting at the bars center you can safely use offset to specify only the offset relative to the bars top center:

plot 'source.dat' using 2:xtic(1),\
         '' u 3,\
         '' u ($0 - dx):2:2 with labels offset -1,1 ,\
         '' u ($0 + dx):3:3 with labels offset 1,1

enter image description here

A second option would be to use the label's alignment: The labels of the red bars are right aligned at the bars right border, the labels of the blue bars are left aligned at the bars left border:

absoluteBoxwidth = 0.8
dx = 1/6.0 * (1 - absoluteBoxwidth)/2.0

plot 'source.dat' using 2:xtic(1),\
         '' u 3,\
         '' u ($0 - dx):2:2 with labels right offset 0,1 ,\
         '' u ($0 + dx):3:3 with labels left offset 0,1

enter image description here

In any case, both options make your script more robust against changes of the input data.

like image 189
Christoph Avatar answered Oct 13 '22 03:10

Christoph


This looks better : enter image description here

plot fuente using 3:xtic(1) ls 1 ti col axis x1y1, '' u 3 ls 2 ti col axis x1y2, '' u ($0-1):3:3 with labels offset -3,1 , '' u ($0-1):2:2 with labels offset 3,1

You had 2 plots commands: only the first one was displayed. Also, script.sh should be a bash script. This is a gnuplot script, so it should have another extension.

like image 2
Eric Duminil Avatar answered Oct 13 '22 03:10

Eric Duminil


The problem is the ti col tab. You need to put it in every option, including labels and not only in bars. The right code is:

plot fuente using 2:xtic(1) ls 1 ti col, '' u 3 ls 2 ti col, '' u 0:2:2 ti col with labels offset -3,1 , '' u 0:3:3 ti col with labels offset 3,1

And that's how the picture is displayed now:

enter image description here

You can also avoid ti col and that is how it would look:

enter image description here

like image 1
Jose Luis de la Flor Avatar answered Oct 13 '22 02:10

Jose Luis de la Flor