GNUPLOT - Can't display values over the bar - gnuplot

I'm trying make simple histogram with this file '.dat'
Moment "Energy consumed (MWh)"
"Apr-16" 2011.4
"May-16" 1869.6
"Jun-16" 1899.0
"Jul-16" 1659.0
"Aug-16" 1740.6
"Sep-16" 1670.0
For this purpose I have written the following script
#!/usr/bin/gnuplot
set term postscript
set terminal pngcairo nocrop enhanced size 700,700 font "arial,18"
set termoption dash
set output out
set boxwidth 0.5 absolute
set border 0
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
set yrange [0.0000 : limite] noreverse nowriteback
show style line
set style line 1 lt 1 lc rgb color lw 1
## Last datafile plotted: "immigration.dat"
plot fuente using (column(0)):2:xtic(1) title titulo ls 1 with boxes, '' using 0:2:2 with labels
In this case out is the output filename, titulo is the label that appears on the top of the image output, limite is the value that I use as biggest value on y-axi, and fuente is the source filename.
The result is this
I am trying display the values over the bar with some offset because I need the values over the bars and not inside the bars. I would need separate I am trying with code like:
plot fuente using (column(0)):2:xtic(1) title titulo ls 1 with boxes, '' using 0:($2 + 0.5):2 with labels
because I have seen many sites where they get it doing $2 + 0.5 but this it doesn't work for me.
What should I do? Please help me I am completly lost. Thanks in advance.

Use the offset parameter for the with labels plotting styles. With this you can add a vertical offset which you specify e.g. in character or graph units:
plot fuente using 0:2:xtic(1) with boxes, '' using 0:2:2 with labels offset 0, char 1
Sidenote: Adding the constant value of 0.5 to the y-value (like $2 + 0.5) doesn't work for you, because the 0.5 is in units of the y-axis, and very small compared to your yrange.

Related

gnuplot: bar charts visualization

My gnuplot script plot a bar graph for 2D data either in monochrome or color format:
set term pngcairo size 800,600
set termoption noenhanced
set tics font "Helvetica,10"
#set xtics noenhanced
set ylabel "Fraction, %"
set xlabel "H-bond donor/aceptor, residue"
set yrange [0:1]
set ytics 0.1
set grid y
set key off
set boxwidth 0.9
set style fill solid 0.5
# TWO OPTIONS FOR BAR VISUALISATIONS!! NB: ADD HERE TRIGGER FROM COLOR_DATA TRIGGER
# 1 - use it with non-colored bars"
#plot "\$data" using 0:2:xtic(1) with boxes, "" using 0:2:2 with labels offset 0,1
# 2 - or use it with colored bars:
plot \$data using 0:2:3:xtic(1) with boxes lc rgb var, \
'' using 0:2:2 with labels offset 0,1
The problem when I have just one bar, one the graph it occupiers all the graph on X:
Would it be possible to set some minimum dimension for the bars to make the dimensions of a single bar similar for a situations with two bars, for instance:
My understanding is the following: if you have only one box:
maybe gnuplot tries to autoscale and the automatic boxwidth is small relative to the autorange (hence just a thin line).
if you set a certain boxwidth, autoscale with scale to the given boxwidth (hence the graph filled with the box).
you could set a fixed xrange, but then you are loosing the benefits of autoscale. Instead you can use set offets (check help offsets).
if you have more than 1 box autoscale will work.
Script:
### boxwidth with boxes style
reset session
$Data1 <<EOD
1 Abc
EOD
$Data2 <<EOD
1 Abc
2 Xyz
EOD
set style fill solid 0.3
set key out
set rmargin screen 0.7
set yrange[0:]
set ytics 0.5
set multiplot layout 4,1
plot $Data1 u 0:1:xtic(2) w boxes ti "No special settings"
set boxwidth 0.9
plot $Data1 u 0:1:xtic(2) w boxes ti "set boxwidth"
set offsets 1,1,0,0
plot $Data1 u 0:1:xtic(2) w boxes ti "set offsets"
set offsets 0,0,0,0
plot $Data2 u 0:1:xtic(2) w boxes ti "more than 1 box"
unset multiplot
### end of script
Result:

Set background of boxes in boxplot to a specific color with gnuplot

I've got the following data:
# id min 1st quart median 3rd quart max sum std-dev name
1 0.00029 0.02590 0.06331 0.12910 6.50177 1524.58705 0.13483 spec
2 1.50041 1.59762 1.67226 1.79827 13.45151 26583.69935 0.48373 schema
3 0.00206 0.01292 0.02505 0.09679 116.93156 5337.36854 2.06006 truss
And the following gnuplot script:
set terminal png enhanced background rgb 'white' size 1920,1200 font "Verdana 10"
set output "charts/summary.png"
set boxwidth 0.2 absolute
set title "Validating all data"
set xrange[0:4]
set yrange[0.00005:50]
set logscale y
set grid y
set tics scale 0
set xtics rotate by -45
set xtics nomirror
set ytics nomirror
set border 2
set style fill solid 0.25 border -1
set style data boxplot
plot "data/summary" using 1:3:2:6:5:(0.6):xticlabels(9) with candlesticks title 'Quartiles' whiskerbars, \
'' using 1:4:4:4:4:(0.6) with candlesticks lt -1 notitle
The output of which looks like:
The problem being that I can't figure out how to set the background color of the boxes in the boxplot. I can only seem to turn on and off the fill-in color by removing the set style fill solid 0.25 border -1 line.
Although the name of keyword doesn't suggest so, the linecolor seems to affect the color of the boxes, thus for example:
plot "data/summary" using 1:3:2:6:5:(0.6):xticlabels(9) with candlesticks lc rgb 'blue' title 'Quartiles' whiskerbars, \
'' using 1:4:4:4:4:(0.6) with candlesticks lt -1 notitle

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
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.
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.
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:

plotting two different types of histogram plots in gnuplot on top of each other

I am working with a histogram in gnuplot and I want to put one of the histogram bars behind the other results. I would like the reference bar (Mörk spennubjögunar > 200 kV) to behind the other histogram bars. I have done this in excel before using different y-axis, is there a nice way to do this in gnuplot?
This is the code I am currently working with.
set terminal pngcairo transparent nocrop enhanced size 3200,2400 font "arial,40"
set output "Harmonic_currents_BRE.png"
set key right
set datafile separator ";"
set style line 12 lc rgb '#808080' lt 0 lw 1
set style line 13 lt 0 lw 3
set grid back ls 12
set xrange [-1:20]
set yrange [0:8]
set style data histogram
set style histogram cluster gap 1
set style fill solid border 0
set border lw 2
set boxwidth 0.7
set ylabel "Hlutfall af nafnspennu [%]"
set xlabel "Tíðni [pu 50 Hz base]"
plot "case0.csv" using 2:xticlabels(1) title 'Tilfelli 0',\
"case1.csv" using 2:xticlabels(1) title 'Tilfelli 1',\
"case2.csv" using 2:xticlabels(1) title 'Tilfelli 2',\
"case3.csv" using 2:xticlabels(1) title 'Tilfelli 3',\
"ref.csv" using 2:xticlabels(1) title 'Mörk spennubjögunar > 200 kV'
unset output
unset zeroaxis
unset terminal
I think, the best way is to first plot the reference with boxes with a fixed boxwidth (I used 0.9), and then the clustered histograms:
set style data histogram
set style histogram cluster gap 1
set style fill solid noborder
set boxwidth 0.7
plot "ref.csv" using 0:2:(0.9):xtic(1) with boxes title 'Mörk spennubjögunar > 200 kV',\
for [i=0:3] sprintf("case%d.csv", i) u 2 title sprintf('Tilfelli %d), i)

How to draw a histogram with broken x-axis and string labels?

I am trying to draw a histogram with broken x-axis. I learnt the basic solution from this post.
However, the x-axis of my histogram is based on strings, not number. It's like this:
set terminal pdf
set output "test-bar.pdf"
set boxwidth 1.0 absolute
set style fill solid 1 border 0
set datafile separator ','
set style data histograms
set xtics nomirror rotate by -45
set ylabel 'Normalized Overhead (%)'
set grid ytics
set yrange [0:10]
plot 'test-bar.csv' using 2:xtic(1) lc rgb "#1E90FF" title ''
And the data is listed like this:
expand , 8.63390441828
cut , 6.84657194596
sync , 6.03615235627
fold , 4.22117995557
nl , 3.54228486647
truncate , 2.66222961714
tr , 2.38357169059
stty , 2.28166797757
users , 2.04211869831
factor , 1.81517270821
tac , 1.790947574
uniq , 1.78799489138
mv , 1.75054704603
mktemp , 1.72228202368
dircolors , 1.6974169738
Right now the plot is in this way:
If I want to leverage the broken axis feature, say, insert the broken between stty and users, how can I do that?
Am I clear enough? Thanks.
Although you use xtic labels from your data file, the xtics are placed at integer x-values, starting at 0. Now, you cannot directly set arbitrary x-values when plotting histograms. You must use newhistogram at ... to shift the second part of the histogram further to the right:
split = 8
plot 'test-bar.csv' using 2:xtic(1) every ::0::(split-1) lt 1,\
newhistogram at split+1,\
'' using 2:xtic(1) every ::split lt 1
Drawing of the upper and lower borders as well as the broken axis signs is done as shown in the post you linked. A possible full script could be
set terminal pdf
set output "test-bar.pdf"
set boxwidth 0.5 absolute
set style fill solid 1 border 0
set datafile separator ','
set style data histograms
set style histogram rowstacked
set xtics nomirror rotate by -45
set ylabel 'Normalized Overhead (%)'
set grid ytics
set yrange [0:10]
unset key
set border 2+8
set linetype 1 lc rgb "#1E90FF"
split=8
dx = 0.125
dy = 0.03
do for [i=0:1] {
set arrow 1+i from graph 0,graph i to first split-dx,graph i lt -1 nohead
set arrow 3+i from first split+dx,graph i to graph 1,graph i lt -1 nohead
set arrow 5+i from first split-2*dx,graph i-dy to first split,graph i+dy lt -1 nohead
set arrow 7+i from first split,graph i-dy to split+2*dx,graph i+dy lt -1 nohead
}
plot 'test-bar.csv' using 2:xtic(1) every ::0::(split-1) lt 1,\
newhistogram at split+1,\
'' using 2:xtic(1) every ::split lt 1
Alternatively, if you don't add or stack more columns, you could use the boxes plotting style, which allows you to use a normal numerical axis.

Resources