【问题标题】:How to add offset to time-based data when plotting in gnuplot在 gnuplot 中绘图时如何为基于时间的数据添加偏移量
【发布时间】:2013-10-05 03:46:08
【问题描述】:

我想从我想使用 gnuplot 绘制的文件中为基于时间的数据添加偏移量。我可以很好地绘制数据,但是当我尝试添加基于时间的偏移量时,图表是空的。

其目的是绘制多个条形图,如下所述,但使用基于时间的数据:Two plots "with boxes" next to each other with gnuplot

数据文件:

00:00 7719
01:00 20957
02:00 15989
03:00 9711
04:00 1782
05:00 871
06:00 4820
07:00 860
08:00 873
09:00 848
10:00 879
11:00 726
12:00 944
13:00 924
14:00 996
15:00 806
16:00 848
17:00 967
18:00 2277
19:00 2668
20:00 32183
21:00 14414
22:00 20426
23:00 16140

我正在尝试使用此代码绘制数据:

set xdata time
set timefmt "%H:%S"
set format x "%H"
set style fill solid 0.6 border -1
set boxwidth 0.3 relative
plot ["00:00":"23:30"] 'data.dat' using ($1-0.3):2 with boxes, \
  'data.dat' using ($1+0.3):2 with boxes

这只是一个测试 - 真实的数据文件有额外的数据列,我正在尝试使用偏移量将这些框彼此相邻放置,但是我对基于时间的数据和偏移量没有运气。

没有偏移,代码就可以了:

set xdata time
set timefmt "%H:%S"
set format x "%H"
set style fill solid 0.6 border -1
set boxwidth 0.3 relative
plot ["00:00":"23:30"] 'data.dat' using 1:2 with boxes

【问题讨论】:

    标签: gnuplot


    【解决方案1】:

    对于使用 timedata 的计算,您必须使用 timecolumn() 函数,该函数根据 set timefmt 设置从列中解析时间字符串。结果是以秒为单位的时间戳。因此,对于偏移量,您必须使用以秒为单位的相应时间:

    set xdata time
    set timefmt "%H:%S"
    set format x "%H"
    set style fill solid 0.6 border -1
    set boxwidth 0.3 relative
    set xrange["00:00":"23:30"]
    set style data boxes
    plot 'data.dat' using 1:2, \
         '' using (timecolumn(1)+60*20):($2*0.5), \
         '' using (timecolumn(1)+60*40):($2*0.7)
    

    这给出了:

    作为另一种变体,您可以使用histogram 样式,并使用strftimetimecolumn 格式化xtic 标签:

    set timefmt "%H:%S"
    set style fill solid 0.6 border -1
    set style data histogram
    set style histogram clustered gap 1
    plot 'data.dat' using 2:xtic(strftime('%H', timecolumn(1))), \
         '' using ($2*0.5), \
         '' using ($2*0.7)
    

    这给了:

    set style histogram clustered gap 0 不会在两个数据块之间进行分隔。

    为了控制tic标签,你可以使用类似的东西

    plot 'data.dat' using 2:xtic((int($0) % 2 == 0) ? strftime('%H', timecolumn(1)) : '')
    

    仅打印数据文件中每隔一个条目的标签($0 指的是行号)。

    【讨论】:

    • 非常感谢,这正是我想要的。很抱歉,我没有代表投票赞成你的答案:-(
    • @user2846740 您可以接受这个答案来感谢他/她的工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    相关资源
    最近更新 更多