Gnuplot,跳过timedat抽搐,直方图

时间:2016-12-27 14:18:04

标签: gnuplot histogram time-format

所以,我需要按日期制作数据的直方图,但我有xticlabel重叠的问题,因此,我试图找到一个解决方法,如何跳过xtics以避免重叠。考虑到日期不是整数抽搐,我试图以这种方式解决它:

.dat文件

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

是否可以按第一列计算xtic,但是在第二列中显示值而不是第一列中的值?

我的代码:

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 个答案:

答案 0 :(得分:2)

在提出这样一个模糊的问题之前,请务必将脚本减少到最低限度,以便重现问题。

删除所有不必要的东西并修复绘图命令后,我最终得到的结果是:

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

在这里,您已经看到一个选项可以通过旋转它们来避免重叠更长的标签。

另一种可能性是跳过每个第n个xticlabel。此时你必须了解gnuplot如何创建直方图。直方图不使用传统的数字轴,因此您不能像绘制线条时那样简单地使用日期。但是gnuplot将每个条形簇放在整数x位置,例如xtic(2)您使用第二列中给出的字符串标记每个集群。

表达式xtic(2)xticlabel(2)的捷径,表示xticlabel(stringcolumn(2))。您可以在此处使用任何产生字符串的表达式,包括条件,而不是使用第二列中的字符串。要仅绘制每个第二个标签,请使用int($0) % 2 == 0检查行号是偶数还是奇数,并使用空字符串或第二列中的字符串:

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

enter image description here