Module: Gnuplot

Defined in:
lib/gnuplot.rb,
lib/gnuplot/version.rb

Overview

Methods and variables for interacting with the GNUPLOT process. Most of these methods are for sending data to a GNUPLOT process, not for reading from it. Some of the methods are implemented as added methods to the built in classes.

Defined Under Namespace

Classes: DataSet, Plot, SPlot

Constant Summary collapse

TERMINAL =

Holds the map of file extensions to GNUPLOT terminals.

Hash.new { |h, k| h[k] = k }.update(
  'dat' => nil,
  'jpg' => 'jpeg',
  'ps'  => 'postscript'
)
VERSION =
'2.3.5.3'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.gnuplot(persist = true) ⇒ Object

Finds the path to the GNUPLOT executable. The name of the executable can be specified using the #gnuplot= accessor or the RB_GNUPLOT environment variable but will default to the command gnuplot.

Adds the persist flag to the GNUPLOT executable if persist is true.

Returns the path to the GNUPLOT executable or raises an error if one cannot be found.



41
42
43
44
45
46
# File 'lib/gnuplot.rb', line 41

def gnuplot(persist = true)
  @gnuplot ||= which(ENV['RB_GNUPLOT'] || 'gnuplot')
  raise 'GNUPLOT executable not found' unless @gnuplot

  "#{@gnuplot}#{' -persist' if persist}"
end

Class Method Details

.open(persist = true, mode = nil, opts = {}) ⇒ Object

Opens a GNUPLOT process via Gnuplot.gnuplot (passing the persist flag) and yields the IO object associated with the GNUPLOT process to the block.

See the GNUPLOT documentation for information on the persist flag.



53
54
55
# File 'lib/gnuplot.rb', line 53

def open(persist = true, mode = nil, opts = {})
  IO.popen(gnuplot(persist), mode || 'w', opts) { |gp| yield gp }
end

.plot(persist = true, *args, &block) ⇒ Object

Wraps the calls to Gnuplot.open and Gnuplot::Plot.new.



58
59
60
61
# File 'lib/gnuplot.rb', line 58

def plot(persist = true, *args, &block)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  open(persist, 'w', opts) { |gp| Plot.new(gp, *args, &block) }
end

.plot_to(file, persist = false, *args, &block) ⇒ Object

If file is a file name that has a Gnuplot.terminal associated with it, the GNUPLOT process’s terminal will be set accordingly and its output redirected to file.

If file is an IO object or doesn’t have a terminal associated with it, the GNUPLOT process’s output will simply be written to file.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gnuplot.rb', line 69

def plot_to(file, persist = false, *args, &block)
  io, file = file, nil if file.respond_to?(:write)

  if file and term = terminal(File.extname(file))
    plot(persist, *args) { |plot|
      block[plot]

      plot.terminal term unless plot[:terminal] || plot[:term]
      plot.output file.to_s unless plot[:output] || plot[:out]
    }
  else
    begin
      io ||= File.open(file, 'w')
      Plot.new(io, *args, &block)
    ensure
      io.close if file && io
    end
  end
end

.terminal(ext) ⇒ Object

Maps file extension (with optional dot) to GNUPLOT terminal (cf. TERMINAL).



24
25
26
# File 'lib/gnuplot.rb', line 24

def terminal(ext)
  TERMINAL[ext.to_s.sub(/\A\./, '')]
end

.which(bin) ⇒ Object

Delegates to File.which, but String#strip’s bin first.



29
30
31
# File 'lib/gnuplot.rb', line 29

def which(bin)
  File.which(bin.strip)
end