使用Ggplot绘制时间序列上的出现次数

时间:2012-12-03 17:59:39

标签: gnuplot

我试图在一些其他图表上绘制一个事件的出现,这个图表显示一个过程花了多少时间来查看是否存在相关性,但我无法弄清楚如何使gnuplot正确地对数据求和。由于数据不是数字,我在应用与绘制直方图相关的材料时遇到了麻烦。这是我的数据:

"2012-05-15 08:12:49","foo"
"2012-05-15 08:13:01","foo"
"2012-05-15 08:13:58","foo"
"2012-05-15 08:14:03","foo"
"2012-05-15 08:14:10","foo"
"2012-05-15 08:14:17","foo"
"2012-05-15 08:14:33","foo"
"2012-05-15 08:14:35","foo"

...

"2012-05-15 10:31:51","foo"
"2012-05-15 10:32:02","foo"
"2012-05-15 10:32:03","foo"
"2012-05-15 10:32:07","foo"
"2012-05-15 10:32:09","foo"
"2012-05-15 10:32:15","foo"

这是数据文件的范围,我希望有一个图表,其中一行代表当时发生的事件数

(所以我可以将其覆盖到我的其他绘图处理时间图上)

这可能吗?

编辑:到目前为止,我已经尝试了几种选项组合,但没有一种产生任何可读的图形,here是我正在使用的gnuplot文件,type1.csv type2.csv来自我正在重叠的图表。 small_report.csv是'occurrence'数据的1000行提取。

2 个答案:

答案 0 :(得分:2)

使用python 2.7计算特定日期的次数非常容易:

from collections import Counter
with open('datafile') as fin:
    c = Counter(line.split()[0][1:] for line in fin)

for k,v in sorted(c.items()):
    print k,v

如果你没有python 2.7,你可以在早期版本中使用defaultdict来模仿它:

from collections import defaultdict
with open('datafile') as fin:
    c = defaultdict(int)
    for line in fin:
        c[ line.split()[0][1:] ] += 1

for k,v in sorted(c.items()):
    print k,v

现在你可以用它来制作一个情节:

set timefmt '%Y-%m-%d'
set xdata time
plot "<python pythonscript.py" u 1:2

答案 1 :(得分:2)

这里的诀窍是记住在为x轴(binning)进行数学运算时使用timecolumn()。

RewriteRule ^watch/(.*) watch.php?v=$1 [NC,L]

上面的示例将给定binwidth内的出现次数相加,并在时间轴上绘制它们。

set xdata time
set timefmt "[%Y-%m-%y %H:%M:%S"
binwidth = 30  #30 second bin
bin(x,width) = width*floor(x/width)
plot "testdata.log" using (bin(timecolumn(1),binwidth)):(1.0) smooth frequency with boxes