Class: Numo::Gnuplot

Inherits:
Object
  • Object
show all
Defined in:
lib/numo/gnuplot.rb

Defined Under Namespace

Modules: OptArg Classes: FitItem, ImageData, NotePlot, PlotData, PlotItem, RgbAlphaData, RgbImageData, SPlotItem, SPlotRecord

Constant Summary collapse

VERSION =
"0.2.4"
POOL =
[]
DATA_FORMAT =
"%.7g"
KNOWN_EXT =
{"ps"=>"postscript","jpg"=>"jpeg"}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gnuplot_command = "gnuplot") ⇒ Gnuplot

Returns a new instance of Gnuplot.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/numo/gnuplot.rb', line 54

def initialize(gnuplot_command="gnuplot")
  @history = []
  @debug = false
  r0,@iow = IO.pipe
  @ior,w2 = IO.pipe
  IO.popen(gnuplot_command,:in=>r0,:err=>w2)
  r0.close
  w2.close
  @gnuplot_version = send_cmd("print GPVAL_VERSION")[0].chomp
  if /\.(\w+)$/ =~ (filename = ENV['NUMO_GNUPLOT_OUTPUT'])
    ext = $1
    ext = KNOWN_EXT[ext] || ext
    opts = ENV['NUMO_GNUPLOT_OPTION'] || ''
    set terminal:[ext,opts]
    set output:filename
  end
end

Instance Attribute Details

#gnuplot_versionObject (readonly)

Returns the value of attribute gnuplot_version.



74
75
76
# File 'lib/numo/gnuplot.rb', line 74

def gnuplot_version
  @gnuplot_version
end

#historyObject (readonly)

Returns the value of attribute history.



72
73
74
# File 'lib/numo/gnuplot.rb', line 72

def history
  @history
end

#last_messageObject (readonly)

Returns the value of attribute last_message.



73
74
75
# File 'lib/numo/gnuplot.rb', line 73

def last_message
  @last_message
end

Class Method Details

.defaultObject



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

def self.default
  POOL[0] ||= self.new
end

Instance Method Details

#clearObject

The ‘clear` command erases the current screen or output device as specified by `set output`. This usually generates a formfeed on hardcopy devices.



197
198
199
200
# File 'lib/numo/gnuplot.rb', line 197

def clear
  send_cmd "clear"
  nil
end

#debug_offObject

turn off debug



257
258
259
# File 'lib/numo/gnuplot.rb', line 257

def debug_off
  @debug = false
end

#debug_onObject

turn on debug



252
253
254
# File 'lib/numo/gnuplot.rb', line 252

def debug_on
  @debug = true
end

#exitObject

The ‘exit` and `quit` commands will exit `gnuplot`.



203
204
205
206
# File 'lib/numo/gnuplot.rb', line 203

def exit
  send_cmd "exit"
  nil
end

#fit(*args) ⇒ Object

The ‘fit` command can fit a user-supplied expression to a set of data points (x,z) or (x,y,z), using an implementation of the nonlinear least-squares (NLLS) Marquardt-Levenberg algorithm.



109
110
111
112
113
114
115
# File 'lib/numo/gnuplot.rb', line 109

def fit(*args)
  range, items = parse_fit_args(args)
  r = range.map{|x| "#{x} "}.join
  c = items.cmd_str
  puts send_cmd("fit #{r}#{c}")
  nil
end

#help(s = nil) ⇒ Object

The ‘help` command displays built-in help.



146
147
148
# File 'lib/numo/gnuplot.rb', line 146

def help(s=nil)
  puts send_cmd "help #{s}\n\n"
end

#load(filename) ⇒ Object

The ‘load` command executes each line of the specified input file.



174
175
176
177
# File 'lib/numo/gnuplot.rb', line 174

def load(filename)
  run "load #{OptArg.quote(filename)}"
  nil
end

#lower_plot(plot_window_nb = nil) ⇒ Object Also known as: lower

The ‘lower` command lowers plot window(s)



189
190
191
192
# File 'lib/numo/gnuplot.rb', line 189

def lower_plot(plot_window_nb=nil)
  send_cmd "lower #{plot_window_nb}"
  nil
end

#output(filename, **opts) ⇒ Object

output current plot to file with terminal setting from extension (not Gnuplot command)



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/numo/gnuplot.rb', line 234

def output(filename,**opts)
  term = opts.delete(:term) || opts.delete(:terminal)
  if term.nil? && /\.(\w+)$/ =~ filename
    term = $1
  end
  term = KNOWN_EXT[term] || term
  if term.nil?
    kernel_raise GnuplotError,"file extension is not given"
  end
  set :terminal, term, *opts
  set output:filename
  refresh
  unset :terminal
  unset :output
end

#pause(*args) ⇒ Object

The ‘pause` command used to wait for events on window. Carriage return entry (-1 is given for argument) and text display option is disabled.

pause 10
pause 'mouse'
pause mouse:%w[keypress button1 button2 button3 close any]


168
169
170
171
# File 'lib/numo/gnuplot.rb', line 168

def pause(*args)
  send_cmd("pause #{OptArg.parse(*args)}").join.chomp
  nil
end

#plot(*args) ⇒ Object

draw 2D functions and data.



77
78
79
80
81
# File 'lib/numo/gnuplot.rb', line 77

def plot(*args)
  contents = parse_plot_args(PlotItem,args)
  _plot_splot("plot",contents)
  nil
end

#quitObject

The ‘exit` and `quit` commands will exit `gnuplot`.



209
210
211
212
# File 'lib/numo/gnuplot.rb', line 209

def quit
  send_cmd "quit"
  nil
end

#raise_plot(plot_window_nb = nil) ⇒ Object Also known as: raise

The ‘raise` command raises plot window(s)



182
183
184
185
# File 'lib/numo/gnuplot.rb', line 182

def raise_plot(plot_window_nb=nil)
  send_cmd "raise #{plot_window_nb}"
  nil
end

#refreshObject

The ‘refresh` reformats and redraws the current plot using the data already read in.



216
217
218
219
# File 'lib/numo/gnuplot.rb', line 216

def refresh
  send_cmd "refresh"
  nil
end

#replot(arg = nil) ⇒ Object

replot is not recommended, use refresh



101
102
103
104
# File 'lib/numo/gnuplot.rb', line 101

def replot(arg=nil)
  run "replot #{arg}", @last_data
  nil
end

#reset(x = nil) ⇒ Object

The ‘reset` command causes all graph-related options that can be

set with the `set` command to take on their default values.


157
158
159
160
# File 'lib/numo/gnuplot.rb', line 157

def reset(x=nil)
  run "reset #{x}"
  nil
end

#run(s, data = nil) ⇒ Object

private methods



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/numo/gnuplot.rb', line 291

def run(s,data=nil)
  res = send_cmd(s,data)
  if !res.empty?
    if /.*?End\sof\sanimation\ssequence.*?/im =~ res.to_s
      return nil
    end
    if res.size < 7
      if /^\s*(line \d+: )?warning:/i =~ res[0]
        $stderr.puts res.join.strip
        return nil
      else
        msg = "\n"+res.join.strip
      end
    else
      msg = "\n"+res[0..5].join.strip+"\n :\n"
    end
    kernel_raise GnuplotError,msg
  end
  nil
end

#send(cmd) ⇒ Object

send command-line string to Gnuplot directly



262
263
264
# File 'lib/numo/gnuplot.rb', line 262

def send(cmd)
  puts send_cmd(cmd)
end

#set(*args) ⇒ Object

The ‘set` command is used to set lots of options.



134
135
136
137
# File 'lib/numo/gnuplot.rb', line 134

def set(*args)
  run "set #{OptArg.parse(*args)}"
  nil
end

#show(*args) ⇒ Object

The ‘show` command shows their settings.



151
152
153
# File 'lib/numo/gnuplot.rb', line 151

def show(*args)
  puts send_cmd "show #{OptArg.parse(*args)}"
end

#splot(*args) ⇒ Object

draws 2D projections of 3D surfaces and data.



84
85
86
87
88
# File 'lib/numo/gnuplot.rb', line 84

def splot(*args)
  contents = parse_plot_args(SPlotItem,args)
  _plot_splot("splot",contents)
  nil
end

#stats(filename, *args) ⇒ Object

This command prepares a statistical summary of the data in one or two columns of a file.



127
128
129
130
131
# File 'lib/numo/gnuplot.rb', line 127

def stats(filename,*args)
  fn = OptArg.quote(filename)
  opt = OptArg.parse(*args)
  puts send_cmd "stats #{fn} #{opt}"
end

#to_sObject

for irb workspace name



285
286
287
# File 'lib/numo/gnuplot.rb', line 285

def to_s
  "gnuplot"
end

#unset(*args) ⇒ Object

The ‘unset` command is used to return to their default state.



140
141
142
143
# File 'lib/numo/gnuplot.rb', line 140

def unset(*args)
  run "unset #{OptArg.parse(*args)}"
  nil
end

#update(*filenames) ⇒ Object

This command writes the current values of the fit parameters into the given file, formatted as an initial-value file (as described in the ‘fit`section). This is useful for saving the current values for later use or for restarting a converged or stopped fit.



121
122
123
# File 'lib/numo/gnuplot.rb', line 121

def update(*filenames)
  puts send_cmd("update "+filenames.map{|f| OptArg.quote(f)}.join(" "))
end

#var(name) ⇒ Object

‘var` returns Gnuplot variable (not Gnuplot command)



222
223
224
225
226
227
228
# File 'lib/numo/gnuplot.rb', line 222

def var(name)
  res = send_cmd("print #{name}").join("").chomp
  if /undefined variable:/ =~ res
    kernel_raise GnuplotError,res.strip
  end
  res
end