# Gnuplot timeseries

**URL:** <https://www.ruby-forum.com/t/gnuplot-timeseries/166221>\
**Category:** Ruby\
**Created:** [April 22, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/gnuplot-timeseries/166221 "2009-04-22T23:16:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robredford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ea924/32.png) [@robredford](https://www.ruby-forum.com/u/robredford)\
**Post date:** [April 22, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/gnuplot-timeseries/166221/1 "2009-04-22T23:16:23Z")

</div>

Does anyone have an example of plotting a timeseries with gnuplot and  
setting the xrange? Why doesn’t the following snippet work as expected?  
It produces the attached.

require ‘rubygems’  
require ‘gnuplot’  
require ‘date’  
obs\_times = Array.new  
data = Array.new  
24.times { |h| obs\_times \<\< DateTime.new( 2009, 1, 1, h ); data \<\<  
rand() }

# doesn’t work either

# 24.times { |h| obs\_times \<\< “2009-1-1 #{h}:00:00”; data \<\<

rand() }

Gnuplot.open do |gp|  
Gnuplot::Plot.new( gp ) do |plot|

# causes error plot.xrange

“[“2009-01-01T00:00:00+00:00”:“2009-01-01T23:59:00+00:00”]”  
plot.data \<\< Gnuplot::DataSet.new( [obs\_times, data] ) do |ds|  
ds.with = “points”  
ds.linewidth = 4  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![robredford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ea924/32.png) [@robredford](https://www.ruby-forum.com/u/robredford)\
**Post date:** [April 22, 2009, 11:41pm UTC](https://www.ruby-forum.com/t/gnuplot-timeseries/166221/2 "2009-04-22T23:41:06Z")

</div>

> # causes error plot.xrange
> 
> “["2009-01-01T00:00:00+00:00":"2009-01-01T23:59:00+00:00"]”

I’m not sure where the +00:00 comes from.

This post…

[O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/ruby/blog/2008/03/cruisecontrol_charts.html)

…shows how to format the time and build a range:

def timefmt; ‘%Y/%d/%m-%H:%M’; end

def fetch\_codelines(stat, fields)  
return stat.values\_at(\*fields).map{|values| values[‘codelines’] }.sum  
end

def ftime(timestamp)  
Time.at(timestamp).strftime(timefmt)  
end  
…  
plot.xrange “[‘#{ ftime(mini) }’:‘#{ ftime(maxi) }’]”

I admit the documentation on that technique, in both Gnuplot.rb and  
gnuplot,  
are very scarce!

---

<div class="post-metadata">

**Author:** ![robredford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7ea924/32.png) [@robredford](https://www.ruby-forum.com/u/robredford)\
**Post date:** [April 23, 2009, 12:02am UTC](https://www.ruby-forum.com/t/gnuplot-timeseries/166221/3 "2009-04-23T00:02:50Z")

</div>

Phlip wrote:

> > # causes error plot.xrange
> > 
> > “["2009-01-01T00:00:00+00:00":"2009-01-01T23:59:00+00:00"]”
> 
> I’m not sure where the +00:00 comes from.

That’s the default format of DateTime.new.to\_s

> This post…
> 
> [O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/ruby/blog/2008/03/cruisecontrol_charts.html)
> 
> …shows how to format the time and build a range:
> 
> def timefmt; ‘%Y/%d/%m-%H:%M’; end
> 
> def fetch\_codelines(stat, fields)  
> return stat.values\_at(\*fields).map{|values| values[‘codelines’] }.sum  
> end
> 
> def ftime(timestamp)  
> Time.at(timestamp).strftime(timefmt)  
> end  
> …  
> plot.xrange “[‘#{ ftime(mini) }’:‘#{ ftime(maxi) }’]”
> 
> I admit the documentation on that technique, in both Gnuplot.rb and  
> gnuplot,  
> are very scarce!

Thanks for the ideas. Ruby originated gnuplot examples are so scarce  
that I’ve been looking (in vain) for another package that as lightweight  
but better documented.
