我想随时间绘制堆积的直方图。结果证明这与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
Run Code Online (Sandbox Code Playgroud)
前两列以空格分隔,其余列以制表符分隔。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'
Run Code Online (Sandbox Code Playgroud)
上面的脚本会导致一个错误: Need full using spec for x time data。但是,如果未设置 xdata,它会起作用。
set xdata time在你的情况下,这部分确实是错误的。
直方图工作从其他绘图风格有点不同:这些箱子被放置在整x值0,1,2等,并得到一个自定义标签,而你的情况是包含在第一列中的时间信息。所以 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'
Run Code Online (Sandbox Code Playgroud)

如果要更改用于标签的时间格式,则必须手动解析时间字符串。xtic(1)相当于xtic(strcol(1))。strcol(1)您可以使用strptime和处理此数据,而不是使用包含第一列中作为字符串的信息的 ,strftime以更改显示的时间信息:
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'
Run Code Online (Sandbox Code Playgroud)
