Class: NumPlot::Plotter

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

Overview

The plotter class. To plot something, you need to create a Plotter object, set parameters to the object, add data sets to the object, and call #plot as in the example below.

Examples:

# An array of data to plot
data =  -2.0.step(2.0, 0.05).map{|x| [x, Math.sin(x)]}
# Create a new Plotter object
plotter = NumPlot::Plotter.new
# Set parameters
plotter.set("term", "png")
plotter.output("sin.png")
plotter.xrange -2.0 .. 2.0
# Set datasets
plotter.datasets << NumPlot::Dataset.rows(data, title: "sin(x)", with: line)
# Plot (output to sin.png)
plotter.plot

Constant Summary

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Conversion

convert_color, convert_font, convert_range, escape_label, quote, quote_label, range_bound, range_pair

Constructor Details

- (Plotter) initialize(plot_command = "plot")

Create a new Plotter object. You can specifiy the plot command (plot/splot) by plot_command parameter.

Parameters:

  • plot_command (String) (defaults to: "plot")

    “plot” or “splot”



254
255
256
257
258
# File 'lib/numplot.rb', line 254

def initialize(plot_command = "plot")
  @plot_command = plot_command
  @commands = []
  @datasets = []
end

Instance Attribute Details

- (Array) datasets (readonly)

Deprecated.

An array of all data sets.

Returns:

  • (Array)


263
264
265
# File 'lib/numplot.rb', line 263

def datasets
  @datasets
end

Instance Method Details

- (self) add_dataset(dataset) Also known as: <<

Add a dataset to the plotter.

Parameters:

Returns:

  • (self)


300
301
302
303
# File 'lib/numplot.rb', line 300

def add_dataset(dataset)
  @datasets << dataset
  self
end

- (Object) disable_enhanced_text_mode

Disable an enhanced text mode of gnuplot.



356
357
358
359
# File 'lib/numplot.rb', line 356

def disable_enhanced_text_mode
  @enhanced = false
  set("termoption", "noenhanced")
end

- (Object) enable_enhanced_text_mode

Enable an enhanced text mode of gnuplot.



350
351
352
353
# File 'lib/numplot.rb', line 350

def enable_enhanced_text_mode
  @enhanced = true
  set("termoption", "enhanced")
end

- (void) output(fname)

This method returns an undefined value.

Set output target. You can specify target file name.

Parameters:

  • fname (String, nil)

    name of target file



396
397
398
399
400
401
402
# File 'lib/numplot.rb', line 396

def output(fname)
  if fname.nil?
    set("output")
  else
    set("output", "\"#{fname}\"")
  end
end

- (void) plot(opts = {}) - (void) plot(io)

Plot given datasets with given parameters.

Overloads:

  • - (void) plot(opts = {})

    This method returns an undefined value.

    Open a gnuplot process and write commands and data to the process. You get the result of plotting on a window or an image file

    You can specify the following options:

    • :persist - Add -persist to gnuplot command. The default value is true.

    • :waiting - Wait a gnuplot process to be terminate. The default value is true. If this value is true and the diagram is drawed on a new window, the ruby program is stopped until the window is closed. On the other hand, if this value is false, the ruby program continue although the window remains opened.

    • :gnuplot_command - gnuplot command name.

    Parameters:

    • opts (defaults to: {})

      options

  • - (void) plot(io)

    This method returns an undefined value.

    Output all commands and data to IO object.

    Normally, you specify NumPlot::Process object as io. For debug use, you can specify the StringIO object as io.

    Parameters:

    • io (#write, #puts)

      output target



432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/numplot.rb', line 432

def plot(arg={})
  
  if Hash === arg
    Process.open(arg.fetch(:persist, true),
                 arg.fetch(:waiting, true),
                 arg.fetch(:gnuplot_command, "gnuplot")) do |pr|
      plot_to_io(pr)
    end
  else
    plot_to_io(arg)
  end
end

- (self) set(key, *values)

Gnuplot “set” command. This method is generic, but you can use some useful methods like #xrange and #xlabel for “set” command.

Parameters:

  • key (String)

    the first argument for gnuplot “set” command like “term”, “output”.

  • values (Array<String>)

    the rest arguments for gnuplot “set” command

Returns:

  • (self)


283
284
285
# File 'lib/numplot.rb', line 283

def set(key, *values)
  add_command("set #{key} #{values.join(' ')}")
end

- (self) unset(key, *values)

Gnuplot “unset” command.

Parameters:

  • key (String)

    the first argument for gnuplot “unset” command

  • values (Array<String>)

    the rest arguments for gnuplot “unset” command

Returns:

  • (self)

See Also:



293
294
295
# File 'lib/numplot.rb', line 293

def unset(key, *values)
  add_command("unset #{key} #{values.join(' ')}")
end

- (self) x2label(label, opts = {})

Set x2label parameter. You can specify the following options as a hash:

  • offset: the label is moved by the given offset

  • font: font info Font

  • textcolor: text info RGB (tc: is also available)

  • enhanced: or noenhanced: true or false

  • rotate: (TODO: FIX IT) OR norotate: true

Parameters:

  • label (String)

    label string

  • opts ({Symbol => Object}) (defaults to: {})

    options

Returns:

  • (self)


383
# File 'lib/numplot.rb', line 383

def x2label(*args); _label("x2label", *args); end

- (self) x2range(range, *opts)

Set x2range parameter. You can specify the range with one of three styles.

  • a string such as: “[-1.0:1.0]” or “[:2.0]”

  • a range such as: -1.0 .. 1.0 or -Float::INFINITY .. 2.0

  • an array such as: [-1.0, 1.0] or [nil, 2.0]

You can also specify the following options as args:

  • reverse: or noreverse: true or false

  • writeback: or nowriteback: true or false

Parameters:

  • range (String, Range, Array)

    the range of x axis

  • opts (Array<String>)

    options

Returns:

  • (self)


332
# File 'lib/numplot.rb', line 332

def x2range(*args); _range("x2range", *args); end

- (self) xlabel(label, opts = {})

Set xlabel parameter. You can specify the following options as a hash:

  • offset: the label is moved by the given offset

  • font: font info Font

  • textcolor: text info RGB (tc: is also available)

  • enhanced: or noenhanced: true or false

  • rotate: (TODO: FIX IT) OR norotate: true

Parameters:

  • label (String)

    label string

  • opts ({Symbol => Object}) (defaults to: {})

    options

Returns:

  • (self)


377
# File 'lib/numplot.rb', line 377

def xlabel(*args); _label("xlabel", *args); end

- (self) xrange(range, *opts)

Set xrange parameter. You can specify the range with one of three styles.

  • a string such as: “[-1.0:1.0]” or “[:2.0]”

  • a range such as: -1.0 .. 1.0 or -Float::INFINITY .. 2.0

  • an array such as: [-1.0, 1.0] or [nil, 2.0]

You can also specify the following options as args:

  • reverse: or noreverse: true or false

  • writeback: or nowriteback: true or false

Parameters:

  • range (String, Range, Array)

    the range of x axis

  • opts (Array<String>)

    options

Returns:

  • (self)


326
# File 'lib/numplot.rb', line 326

def xrange(*args); _range("xrange", *args); end

- (self) y2label(label, opts = {})

Set y2label parameter. You can specify the following options as a hash:

  • offset: the label is moved by the given offset

  • font: font info Font

  • textcolor: text info RGB (tc: is also available)

  • enhanced: or noenhanced: true or false

  • rotate: (TODO: FIX IT) OR norotate: true

Parameters:

  • label (String)

    label string

  • opts ({Symbol => Object}) (defaults to: {})

    options

Returns:

  • (self)


385
# File 'lib/numplot.rb', line 385

def y2label(*args); _label("y2label", *args); end

- (self) y2range(range, *opts)

Set y2range parameter. You can specify the range with one of three styles.

  • a string such as: “[-1.0:1.0]” or “[:2.0]”

  • a range such as: -1.0 .. 1.0 or -Float::INFINITY .. 2.0

  • an array such as: [-1.0, 1.0] or [nil, 2.0]

You can also specify the following options as args:

  • reverse: or noreverse: true or false

  • writeback: or nowriteback: true or false

Parameters:

  • range (String, Range, Array)

    the range of x axis

  • opts (Array<String>)

    options

Returns:

  • (self)


334
# File 'lib/numplot.rb', line 334

def y2range(*args); _range("y2range", *args); end

- (self) ylabel(label, opts = {})

Set ylabel parameter. You can specify the following options as a hash:

  • offset: the label is moved by the given offset

  • font: font info Font

  • textcolor: text info RGB (tc: is also available)

  • enhanced: or noenhanced: true or false

  • rotate: (TODO: FIX IT) OR norotate: true

Parameters:

  • label (String)

    label string

  • opts ({Symbol => Object}) (defaults to: {})

    options

Returns:

  • (self)


379
# File 'lib/numplot.rb', line 379

def ylabel(*args); _label("ylabel", *args); end

- (self) yrange(range, *opts)

Set yrange parameter. You can specify the range with one of three styles.

  • a string such as: “[-1.0:1.0]” or “[:2.0]”

  • a range such as: -1.0 .. 1.0 or -Float::INFINITY .. 2.0

  • an array such as: [-1.0, 1.0] or [nil, 2.0]

You can also specify the following options as args:

  • reverse: or noreverse: true or false

  • writeback: or nowriteback: true or false

Parameters:

  • range (String, Range, Array)

    the range of x axis

  • opts (Array<String>)

    options

Returns:

  • (self)


328
# File 'lib/numplot.rb', line 328

def yrange(*args); _range("yrange", *args); end

- (self) zlabel(label, opts = {})

Set zlabel parameter. You can specify the following options as a hash:

  • offset: the label is moved by the given offset

  • font: font info Font

  • textcolor: text info RGB (tc: is also available)

  • enhanced: or noenhanced: true or false

  • rotate: (TODO: FIX IT) OR norotate: true

Parameters:

  • label (String)

    label string

  • opts ({Symbol => Object}) (defaults to: {})

    options

Returns:

  • (self)


381
# File 'lib/numplot.rb', line 381

def zlabel(*args); _label("zlabel", *args); end

- (self) zrange(range, *opts)

Set zrange parameter. You can specify the range with one of three styles.

  • a string such as: “[-1.0:1.0]” or “[:2.0]”

  • a range such as: -1.0 .. 1.0 or -Float::INFINITY .. 2.0

  • an array such as: [-1.0, 1.0] or [nil, 2.0]

You can also specify the following options as args:

  • reverse: or noreverse: true or false

  • writeback: or nowriteback: true or false

Parameters:

  • range (String, Range, Array)

    the range of x axis

  • opts (Array<String>)

    options

Returns:

  • (self)


330
# File 'lib/numplot.rb', line 330

def zrange(*args); _range("zrange", *args); end