带有gnuplot的时间序列数据的叠加直方图?

时间:2017-02-04 18:57:15

标签: charts gnuplot histogram bar-chart stackedbarseries

我有很多像这样的数据

 callr |    method  | call_count |    day     
 ------+-------------------------+------------
 foo   | find_paths |      10    | 2016-10-10
 bar   | find_paths |      100   | 2016-10-10
 foo   | find_all   |      123   | 2016-10-10
 foo   | list_paths |     2243   | 2016-10-10
 foo   | find_paths |      234   | 2016-10-11
 foo   | collect    |      200   | 2016-10-11
 bar   | collect    |       1    | 2016-10-11
 baz   | collect    |        3   | 2016-10-11
 ...      ...             ...        ...

我想为每种方法创建一个堆叠直方图,显示每天的连续天数和每天的堆积条形码,包括来电者和来电次数。

如果我转换数据,例如

select method, sum(call_count), day from foo where method='collect' group by method, day order by method, day;

我能够获得一个条形图,其中包含一种颜色的方法的所有调用,带有这样的plg文件,例如:

set terminal png
set title "Method: " . first_arg
set output "" . first_arg . ".png"
set datafile separator '|'
set style data boxes
set style fill solid
set boxwidth 0.5
set xdata time
set timefmt "%Y-%m-%d"
set format x "%a %m-%d"
xstart="2016-10-01"
xend="2017-01-01"
set xrange [xstart:xend]
set xlabel "Date" tc ls 8  offset -35, -3
set ylabel "Calls"  tc ls 8

plot '<cat' using 3:4

这样叫:

cat file | gnuplot -p -e "plot '<cat';first_arg='collect'" calls.plg

histogram of all calls

然而,我真正想要的是一种通过调用者在同一种图形中显示细分的方法。我还无法使用gnuplot获得堆积直方图。 我试过的所有内容都抱怨使用声明,例如: &#39;需要完整使用x时间数据的规范&#39;等等。

想要这样的事情,但是随着时间的推移,这些日子一直持续到底。例如。如果那天没有打电话 - 那么没有直方图栏

enter image description here

感谢您提出任何想法

1 个答案:

答案 0 :(得分:2)

使用smooth freqbin()功能合并每天的数据,该功能将纪元时间舍入为天。使用内联for和总和表达式将y轴类别的总和绘制为高度降序的框,以便总和之间的差异等于类别的值。所以,最高的盒子将有高度foo + bar + baz(caller=3),下一个最高的foo + bar(caller=2),最短的就是foo(caller=1)。 / p>

calls

caller  method      call_count  day
foo     find_paths  10          2016-10-10
bar     find_paths  100         2016-10-10
foo     find_all    123         2016-10-10
foo     list_paths  2243        2016-10-10
foo     find_paths  234         2016-10-11
foo     collect     200         2016-10-11
bar     collect     1           2016-10-11
baz     collect     3           2016-10-11

gnuplot脚本:

binwidth = 86400
bin(t) = (t - (int(t) % binwidth))
date_fmt = "%Y-%m-%d"
time = '(bin(timecolumn(4, date_fmt)))'

# Set absolute boxwidth so all boxes get plotted fully. Otherwise boxes at the
# edges of the range can get partially cut off, which I think looks weird.
set boxwidth 3*binwidth/4 absolute

set key rmargin
set xdata time
set xtics binwidth format date_fmt time rotate by -45 out nomirror
set style fill solid border lc rgb "black"

callers = system("awk 'NR != 1 {print $1}' calls \
    | sort | uniq -c | sort -nr | awk '{print $2}'")
# Or, if Unix tools aren't available:
# callers = "foo bar baz"

plot for [caller=words(callers):1:-1] 'calls' \
    u @time:(sum [i=1:caller] \
        strcol("caller") eq word(callers, i) ? column("call_count") : 0) \
    smooth freq w boxes t word(callers, caller)

Calls per day, by caller

我在这里写了一篇关于gnuplot时间序列直方图的更长时间的讨论:Time-series histograms: gnuplot vs matplotlib