GNUPLOT - Two columns histogram with values on top of bars

醉酒当歌 提交于 2019-11-29 14:33:43

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

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

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

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

This looks better :

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.

Jose Luis de la Flor

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:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!