【问题标题】:Stacked histogram over time via gnuplot通过 gnuplot 随时间堆积的直方图
【发布时间】:2014-05-20 00:13:29
【问题描述】:

我想绘制一个随时间变化的堆积直方图。事实证明这与Using gnuplot for stacked histograms 不同。

这是数据:

05/11/2014 10:00:00 1   5   1   
05/12/2014 22:00:00 3   5   1
05/13/2014 13:00:00 4   4   1
05/14/2014 09:00:00 3   4   1
05/15/2014 04:00:00 1   2   1

前两列由空格分隔,其余列由制表符分隔。 x 轴应该是日期和时间。

下面的gnuplot脚本有问题:

set title "Test"
set key invert reverse Left outside
set key autotitle columnheader
set style data histogram
set style histogram rowstacked
set style fill solid border -1
set boxwidth 0.75
set datafile separator '\t'
set xdata time
set timefmt '%M/%D/%Y %H:%M:%S'
set format x '%M/%D %H'
plot 'test.dat' using 2:xtic(1) title 'Col1',  '' using 3 title 'Col2', '' using 4 title 'Col3'

上面的脚本会导致错误:Need full using spec for x time data。但是,如果未设置 xdata,它会起作用。

【问题讨论】:

    标签: gnuplot


    【解决方案1】:

    在您的情况下,set xdata time 部分确实是错误的。

    直方图的工作方式与其他绘图样式有点不同:这些框放置在整数 x 值 012 等处,并获得一个自定义标签,在您的情况下是包含的时间信息在第一列。所以 x 轴不是实时轴。

    以下脚本适用于 4.6.4:

    set title "Test"
    set key invert reverse Left outside
    set style data histogram
    set style histogram rowstacked
    set style fill solid border -1
    set boxwidth 0.75
    set datafile separator '\t'
    
    plot 'test.dat' using 2:xtic(1) title 'Col1',  '' using 3 title 'Col2', '' using 4 title 'Col3'
    

    如果要更改用于标签的时间格式,则必须手动解析时间字符串。 xtic(1) 等价于 xtic(strcol(1))。您可以使用strptimestrftime 处理此数据,以更改显示的时间信息,而不是使用strcol(1),它包含第一列中的信息:

    set title "Test"
    set key invert reverse Left outside
    set style data histogram
    set style histogram rowstacked
    set style fill solid border -1
    set boxwidth 0.75
    set datafile separator '\t'
    
    plot 'test.dat' using 2:xtic(strftime('%m/%d %Hh', strptime('%m/%d/%Y %H:%M:%S', strcol(1)))) title 'Col1',\
         '' using 3 title 'Col2', '' using 4 title 'Col3'
    

    【讨论】:

      【解决方案2】:

      如何保持简单,只使用双引号内的日期和时间值。所以你的数据文件是:

      "05/11/2014 10:00:00" 1   5   1   
      "05/12/2014 22:00:00" 3   5   1
      "05/13/2014 13:00:00" 4   4   1
      "05/14/2014 09:00:00" 3   4   1
      "05/15/2014 04:00:00" 1   2   1
      

      您的绘图脚本将是:

      set title "Test"
      set key invert reverse Left outside
      #set key autotitle columnheader
      set style data histogram
      set style histogram rowstacked
      set style fill solid border -1
      set boxwidth 0.75
      set xtics border in scale 0,0 nomirror rotate by 90  offset character 0, -9, 0
      plot 'test.dat' using 2:xtic(1) title 'Col1',  '' using 3 title 'Col2', '' using 4 title 'Col3'
      

      由此产生的情节将是:

      【讨论】:

      • 如果我只想在 x 轴上显示日期 + 小时怎么办。例如,使用 set format x '%M/%D %H'?
      • 为此,您可以相应地更改数据文件,例如:"05/12/22:00" 1 5 1 而不是 "05/11/2014 10:00:00" 1 5 1
      • 无需更改所有内容的数据文件;)@erictan 正如我在回答中指出的那样:对于直方图,x 轴的行为不依赖于set format x。请参阅我的答案,了解一种无需每次都更改数据文件即可更改 xticlabels 的方法。
      • 那太好了,关于 Gnuplot @Christoph 的一般性问题:Gnuplot 是否继承了所有 C 函数?也就是说,我们可以在编写 Gnuplot 代码时使用任何 C 函数吗?
      • 不,gnuplot 中可用的字符串函数数量非常有限。查看Expressions -> Functions 部分中的文档:gprintfsprintfstrlenstrstrtsubstrstrftimestrptime
      猜你喜欢
      • 2017-05-11
      • 1970-01-01
      • 2020-10-26
      • 2016-12-19
      • 1970-01-01
      • 2017-06-22
      • 2016-02-07
      • 2011-01-15
      • 1970-01-01
      相关资源
      最近更新 更多