1
votes

Pulling my hair for the last few hours. Using GnuplotRB. Trying to plot some time series, and whatever formatting and xrange options I try to use, the X-axis always display 1990 to 2035 (where does this come from??)

Example below with similar data

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)

[Edit] - simplified code further.

I have also done this tweak to actually get some error logging from gnuplot. And I helps indeed pinpointing that xrange is a b*** but I still don't get how to make it work :

$  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'

I don't get the "Warning: empty x range [2013:2013]"

1
Couldn't try the code, but you have a typo in xange ( must be xrange), and the xrange must be given in the same format that you use as timefmt - Christoph
Thx a lot. The typo was a copy paste error :/ - Peter Longfield
I have edited my example above to keep it even simpler. - Peter Longfield
i.imgur.com/GKmmXAo.png ? Even simpler but still failing ? - Peter Longfield

1 Answers

1
votes

From the documentation, it seems that one needs to pass the plot-specific settings to the Plot.new call:

Plot's options are explained in gnuplot doc (pp. 105-181). Plot options are translated into gnuplot format the same way as Dataset's (except adding 'set' before each option).

thus:

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]'
)

Otherwise, gnuplotrb tries to assemble all options passed to Dataset.new in a single plot command (as can be seen in the error message you posted) which then fails:

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" ];