Ruby / Gnuplot / GnuplotRB中的时间轴格式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ruby / Gnuplot / GnuplotRB中的时间轴格式相关的知识,希望对你有一定的参考价值。
最后几个小时拉我的头发。使用GnuplotRB。试图绘制一些时间序列,以及我尝试使用的任何格式和xrange选项,X轴始终显示1990到2035(这来自哪里?)
以下示例具有类似数据
require 'gnuplotrb'
include GnuplotRB
x = ["2013-05-22 02:49:49", "2013-05-22 02:56:49", "2013-05-22 02:56:59"]
y = [ 2,5,9]
p x.first
p x.last
element = Dataset.new(
[x, y],
with:'lines lw 3 lt rgb "black"',
timefmt: "%Y-%m-%d %H:%M:%S",
xdata: 'time',
format_x: "%Y-%m-%d %H:%M:%S",
# xrange: '[ "#{x.first}":"#{x.last}" ]'
xrange: '[ "2013-05-22 02:49:00" : "2013-05-22 02:50:00" ]'
)
myBRTraces = Plot.new(
element
)
p myBRTraces.inspect
myBRTraces.to_png("/media/sf_D_DRIVE/Test/a.svg", size: [1200, 1800], truecolor:true)
[编辑] - 进一步简化代码。
我还做了this tweak实际上从gnuplot获取一些错误记录。我帮助确实指出xrange是一个b ***,但我仍然不知道如何让它工作:
$ ruby test.rb
"2013-05-22 02:49:49"
"2013-05-22 02:56:59"
"#<GnuplotRB::Plot:0x00000001ba21f0 @options=Hamster::Hash[], @datasets=Hamster::Vector[#<GnuplotRB::Dataset:0x00000001ba2100 @type=:datablock, @data=#<GnuplotRB::Datablock:0x00000001ba1f70 @stored_in_file=false, @data="2013-05-22 02:49:49 2\n2013-05-22 02:56:49 5\n2013-05-22 02:56:59 9">, @options=Hamster::Hash[:with => "lines lw 3 lt rgb \"black\"", :timefmt => "%Y-%m-%d %H:%M:%S", :xdata => "time", :format_x => "%Y-%m-%d %H:%M:%S", :xrange => "[ \"2013-05-22 02:49:00\" : \"2013-05-22 02:50:00\" ]"]>], @cmd="plot ">"
/var/lib/gems/2.3.0/gems/gnuplotrb-0.4.0/lib/gnuplotrb/mixins/error_handling.rb:28:in `check_errors': Error in previous command ("Warning: empty x range [2013:2013], adjusting to [1992.87:2033.13]"): "Warning: empty y range [2:2], adjusting to [1.98:2.02]; gnuplot> plot $DATA1 with lines lw 3 lt rgb "black" timefmt "%Y-%m-%d %H:%M:%S" xdata time format x "%Y-%m-%d %H:%M:%S" xrange [ "2013-05-22 02:49:00" : "2013-05-22 02:50:00" ]; line 4: unexpected or unrecognized token" (GnuplotRB::GnuplotError)
from /var/lib/gems/2.3.0/gems/gnuplotrb-0.4.0/lib/gnuplotrb/staff/terminal.rb:183:in `close'
from /var/lib/gems/2.3.0/gems/gnuplotrb-0.4.0/lib/gnuplotrb/plot.rb:85:in `plot'
from /var/lib/gems/2.3.0/gems/gnuplotrb-0.4.0/lib/gnuplotrb/mixins/plottable.rb:111:in `to_specific_term'
from /var/lib/gems/2.3.0/gems/gnuplotrb-0.4.0/lib/gnuplotrb/mixins/plottable.rb:53:in `method_missing'
我没有得到“警告:空x范围[2013:2013]”
答案
从documentation,似乎需要将特定于情节的设置传递给Plot.new
调用:
Plot的选项在gnuplot doc(第105-181页)中有解释。绘图选项以与数据集相同的方式转换为gnuplot格式(除了在每个选项之前添加'set')。
从而:
element = Dataset.new(
[x, y],
with: 'lines lw 3 lt rgb "black"',
using: '1:2'
)
myBRTraces = Plot.new(
element,
timefmt: "%Y-%m-%d %H:%M:%S",
xdata: 'time',
format_x: "%Y-%m-%d %H:%M:%S",
xrange: '[ "2013-05-22 02:49:00" : "2013-05-22 02:50:00" ]',
yrange: '[0:10]'
)
否则,gnuplotrb
会尝试在单个Dataset.new
命令中汇集传递给plot
的所有选项(如您发布的错误消息中所示),然后失败:
plot $DATA1 with lines lw 3 lt rgb "black" timefmt "%Y-%m-%d %H:%M:%S" xdata time format x "%Y-%m-%d %H:%M:%S" xrange [ "2013-05-22 02:49:00" : "2013-05-22 02:50:00" ];
以上是关于Ruby / Gnuplot / GnuplotRB中的时间轴格式的主要内容,如果未能解决你的问题,请参考以下文章