Class: GSLng::Plotter

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/gslng/plotter.rb

Overview

This class encapsulates the communication with the plotting backend: gnuplot.

Plot data is always represented as a Matrix, so you can use Matrix#plot to do a single plot. Otherwise, you can use Matrix#define_plot (taking the same parameters as the previous method) which returns a Plot object. By defining multiple plot objects you can then use #plot passing all plot objects as parameters effectively creating a single output of all plots.

This class works as Singleton, so when you instantiate it the gnuplot process is started. You can also send arbitrary commands to the gnuplot process by using the #<< operator. Read the gnuplot documentation on what are the possible commands you can send.

Defined Under Namespace

Classes: Plot

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlotter

Creates the singleton Plotter object



21
22
23
24
25
# File 'lib/gslng/plotter.rb', line 21

def initialize
  @io = IO.popen('gnuplot', 'w')
  @io.sync = true
  self << "set datafile nofpe_trap"
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



18
19
20
# File 'lib/gslng/plotter.rb', line 18

def io
  @io
end

Instance Method Details

#<<(cmd) ⇒ Object

Send a command to the gnuplot process

Examples:

Setting the xrange

Plotter.instance << "set xrange [0:1]"


41
42
43
# File 'lib/gslng/plotter.rb', line 41

def <<(cmd)
  @io.puts(cmd)
end

#closeObject

Close the pipe. It is desireable to call this in an “ensure” section, to avoid leaving the child gnuplot process there if the main ruby process dies



29
30
31
32
33
34
35
36
# File 'lib/gslng/plotter.rb', line 29

def close
  pid = @io.pid
  @io.flush; @io.close; @io = nil
  begin
  Process.kill 'TERM', pid
  rescue Errno::ESRCH
  end
end

#multiplotObject

Yields the given block enabling and disabling multiplot mode, before and after the yield respectively



57
58
59
60
61
62
# File 'lib/gslng/plotter.rb', line 57

def multiplot
  self << 'set multiplot'
  yield(self)
ensure
  self << 'unset multiplot'
end

#plot(*plots) ⇒ Object

Expects a variable number of Plot objects and creates a single plot out of them



46
47
48
49
# File 'lib/gslng/plotter.rb', line 46

def plot(*plots)
  self << 'plot ' + plots.map(&:command).join(', ')
  plots.each {|p| self.put_data(p.matrix)}
end

#put_data(matrix) ⇒ Object



51
52
53
54
# File 'lib/gslng/plotter.rb', line 51

def put_data(matrix) # @private
  ret = GSLng.backend.gsl_matrix_putdata(matrix.ptr, @io.to_i)
  if (ret != 0) then raise SystemCallError.new("Problem sending data to gnuplot", ret) end
end