class NumPlot::Plotter

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. @example

# 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

Constants

RANGE_OPTIONS

@!visibility private

Attributes

datasets[R]

An array of all data sets. @deprecated @return [Array]

Public Class Methods

new(plot_command = "plot") click to toggle source

Create a new Plotter object. You can specifiy the plot command (plot/splot) by plot_command parameter. @param plot_command “plot” or “splot”

# File lib/numplot.rb, line 254
def initialize(plot_command = "plot")
  @plot_command = plot_command
  @commands = []
  @datasets = []
end

Public Instance Methods

<<(dataset)
Alias for: add_dataset
add_dataset(dataset) click to toggle source

Add a dataset to the plotter. @param dataset a dataset @return [self]

# File lib/numplot.rb, line 300
def add_dataset(dataset)
  @datasets << dataset
  self
end
Also aliased as: <<
disable_enhanced_text_mode() click to toggle source

Disable an enhanced text mode of gnuplot.

# File lib/numplot.rb, line 356
def disable_enhanced_text_mode
  @enhanced = false
  set("termoption", "noenhanced")
end
enable_enhanced_text_mode() click to toggle source

Enable an enhanced text mode of gnuplot.

# File lib/numplot.rb, line 350
def enable_enhanced_text_mode
  @enhanced = true
  set("termoption", "enhanced")
end
output(fname) click to toggle source

Set output target. You can specify target file name. @param fname name of target file @return [void]

# File lib/numplot.rb, line 396
def output(fname)
  if fname.nil?
    set("output")
  else
    set("output", "\"#{fname}\"")
  end
end
plot(arg={}) click to toggle source

Plot given datasets with given parameters. @overload plot(opts={})

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.

@param opts options
@return [void]

@overload plot(io)

Output all commands and data to IO object.

Normally, you specify {Process} object as io.
For debug use, you can specify the StringIO object as io.

@param io[#write, #puts] output target
@return [void]
# 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
set(key, *values) click to toggle source

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

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

@param

# File lib/numplot.rb, line 283
def set(key, *values)
  add_command("set #{key} #{values.join(' ')}")
end
unset(key, *values) click to toggle source

Gnuplot “unset” command. @param key

the first argument for gnuplot "unset" command

@param set @return [self]

# File lib/numplot.rb, line 293
def unset(key, *values)
  add_command("unset #{key} #{values.join(' ')}")
end
x2label(*args) click to toggle source

@macro label

# File lib/numplot.rb, line 383
def x2label(*args); _label("x2label", *args); end
x2range(*args) click to toggle source

@macro range

# File lib/numplot.rb, line 332
def x2range(*args); _range("x2range", *args); end
xlabel(*args) click to toggle source

@macro label

# File lib/numplot.rb, line 377
def xlabel(*args); _label("xlabel", *args); end
xrange(*args) click to toggle source

@macro range

# File lib/numplot.rb, line 326
def xrange(*args); _range("xrange", *args); end
y2label(*args) click to toggle source

@macro label

# File lib/numplot.rb, line 385
def y2label(*args); _label("y2label", *args); end
y2range(*args) click to toggle source

@macro range

# File lib/numplot.rb, line 334
def y2range(*args); _range("y2range", *args); end
ylabel(*args) click to toggle source

@macro label

# File lib/numplot.rb, line 379
def ylabel(*args); _label("ylabel", *args); end
yrange(*args) click to toggle source

@macro range

# File lib/numplot.rb, line 328
def yrange(*args); _range("yrange", *args); end
zlabel(*args) click to toggle source

@macro label

# File lib/numplot.rb, line 381
def zlabel(*args); _label("zlabel", *args); end
zrange(*args) click to toggle source

@macro range

# File lib/numplot.rb, line 330
def zrange(*args); _range("zrange", *args); end

Private Instance Methods

_label(type, *args) click to toggle source
# File lib/numplot.rb, line 387
def _label(type, *args)
  set(type, LabelParser.parse(@enhanced, *args))
end
_range(type, r, opts={}) click to toggle source
# File lib/numplot.rb, line 336
def _range(type, r, opts={})
  gnuplot_options = ""
  RANGE_OPTIONS.each do |opt|
    gnuplot_options << opt.plot_arg(opts)
  end
  
  set(type, convert_range(r), gnuplot_options)
end
add_command(command) click to toggle source

Add a command string to the list of commands. The commands are executed when {#plot} is called. @param command a command to add @return [self]

# File lib/numplot.rb, line 269
def add_command(command)
  @commands << command
  self
end
plot_to_io(pr) click to toggle source

Output all commands and data to IO object.

# File lib/numplot.rb, line 446
def plot_to_io(pr)
  @commands.each{|cmd| pr.puts(cmd) }
  pr.write(@plot_command)
  pr.write(" ")
  pr.write(@datasets.map{|e| e.to_plot_arg(@enhanced) }.join(", "))
  pr.write("\n")
  pr.write(@datasets.map{|e| e.to_plot_dataset }.compact.join("e\n"))
end