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.1"
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.


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

def clear
  send_cmd "clear"
  nil
end

#debug_offObject

turn off debug


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

def debug_off
  @debug = false
end

#debug_onObject

turn on debug


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

def debug_on
  @debug = true
end

#exitObject

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


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

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.


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

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.


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

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.


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

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

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

The `lower` command lowers plot window(s)


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

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)


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

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]

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

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`.


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

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)


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

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.


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

def refresh
  send_cmd "refresh"
  nil
end

#replot(arg = nil) ⇒ Object

replot is not recommended, use refresh


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

def replot(arg=nil)
  run "replot #{arg}\n#{@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.

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

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

#run(s, data = nil) ⇒ Object

private methods


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

def run(s,data=nil)
  res = send_cmd(s,data)
  if !res.empty?
    if res.size < 7
      if res.all?{|x| /^\s*(line \d+: )?warning:/i =~ x}
        $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


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

def send(cmd)
  puts send_cmd(cmd)
end

#set(*args) ⇒ Object

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


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

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

#show(*args) ⇒ Object

The `show` command shows their settings.


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

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.


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

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

#to_sObject

for irb workspace name


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

def to_s
  "gnuplot"
end

#unset(*args) ⇒ Object

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


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

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.


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

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

#var(name) ⇒ Object

`var` returns Gnuplot variable (not Gnuplot command)


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

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