Gnuplot, skipping timedat tics, histogram

1.3k views Asked by At

So, i need to make histogram of data by dates, but i have problem with xticlabel overlapping, so, i'm trying to find a solution how to skip xtics to avoid overlapping. Considering that dates are not integer tics, i was trying to solve it that way:

the .dat file

Time    Dat 1   Dat 2
1   27-12-2016  12  2
2   28-12-2016  13  7
3   29-12-2016  17  2
4   30-12-2016  9   10
....

Is it possible to count xtic by first column, but show values in second column instead of values in first?

my code:

reset
dx=5.
n=2
total_box_width_relative=0.75
gap_width_relative=0.1
d_width=(gap_width_relative+total_box_width_relative)*dx/2.
d_box = total_box_width_relative/n
reset

set term png truecolor font "arial,10" fontscale 1.0 size 800,400
set output "test.png"
set datafile separator "\\t"
set title "Errors"
set print "-"
set xlabel 'x' offset "0", "-1"
set ylabel 'y' offset "1", "-0"
set key invert reverse Left outside 
set key autotitle columnheader
set key samplen 4 spacing 1 width 0 height 0 
set autoscale yfixmax
set yrange [0: ]
set xtics strftime('%d-%m-%Y', "27-12-2016"), 5, strftime('%m-%d-%Y', "15-01-2017")  
set xtics font ", 7" 
set ytics auto font ", 9" 
set y2tics auto font ", 9"
set grid
set style data histogram
set style histogram cluster gap 1
set style fill transparent solid 0.75 noborder
set boxwidth 0.9 relative
set xtic rotate by -45 scale 0
plot 'datfile' u 3:xtic(strftime('%d-%m-%Y', strptime('%m.%d.%Y', stringcolumn(2)))), '' u 4
1

There are 1 answers

2
Christoph On BEST ANSWER

Before asking such a vague question, always reduce the script to a bare minimum which is required to reproduce the problem.

After removing all unnecessary stuff and fixing the plot command, here is what I end up with:

reset
set datafile separator "\t"
set yrange [0:*]
set style fill transparent solid 0.75 noborder
set boxwidth 0.9 relative
set xtic rotate by -45 scale 0
set key autotitle columnheader

set style data histogram
set style histogram cluster gap 1

plot 'file.dat' using 3:xtic(2) t col(2), '' using 4

enter image description here

Here, you already see one option to avoid overlapping of longer tic labels by rotating them.

Another possibility is to skip every n-th xticlabel. At this point you must understand how gnuplot creates histograms. Histograms don't use a conventional numerical axis, so you cannot simply use the dates as you normally would do when plotting lines. But gnuplot puts each bar cluster at an integer x-position and with e.g. xtic(2) you label every cluster with the string as given in the second column.

The expression xtic(2) is a short cut for xticlabel(2), which means xticlabel(stringcolumn(2)). Instead of using exactly the string in the second column, you can use here any expression which yields a string, including conditions. To only plot every second label check if the row number is even or odd with int($0) % 2 == 0 and use and empty string or the string from the second column:

plot 'file.dat' using 3:xtic(int($0)%2 == 0 ? stringcolumn(2) : '') t col(2), '' u 4

enter image description here