Class: GSLng::Plotter
- Inherits:
-
Object
- Object
- GSLng::Plotter
- 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
-
#io ⇒ Object
readonly
Returns the value of attribute io.
Instance Method Summary collapse
-
#<<(cmd) ⇒ Object
Send a command to the gnuplot process.
-
#close ⇒ Object
Close the pipe.
-
#initialize ⇒ Plotter
constructor
Creates the singleton Plotter object.
-
#multiplot ⇒ Object
Yields the given block enabling and disabling multiplot mode, before and after the yield respectively.
-
#plot(*plots) ⇒ Object
Expects a variable number of Plot objects and creates a single plot out of them.
- #put_data(matrix) ⇒ Object
Constructor Details
#initialize ⇒ Plotter
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
#io ⇒ Object (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
41 42 43 |
# File 'lib/gslng/plotter.rb', line 41 def <<(cmd) @io.puts(cmd) end |
#close ⇒ Object
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 |
#multiplot ⇒ Object
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 |