Module: Daru::Plotting::Vector

Included in:
Vector
Defined in:
lib/daru/plotting/vector.rb

Instance Method Summary collapse

Instance Method Details

#plot(opts = {}) ⇒ Object

Plots a Vector with Nyaplot on IRuby using the given options.

Options

type (:scatter, :bar, :histogram), title, x_label, y_label, color(true/false)

Usage

vector = Daru::Vector.new [10,20,30,40], [:one, :two, :three, :four]
vector.plot type: :bar, title: "My first plot", color: true


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/daru/plotting/vector.rb', line 18

def plot opts={}
  options = {
    type: :scatter,
    title: "#{@name}",
    x_label: '',
    y_label: '',
    color: false
  }.merge(opts)

  x_axis = options[:type] == :scatter ? Array.new(@size) { |i| i } : @index.to_a
  plot   = Nyaplot::Plot.new
  plot.width(options[:width])   if options[:width]
  plot.height(options[:height]) if options[:height]
  
  p      = plot.add( options[:type], x_axis, @vector.to_a )
  plot.x_label( options[:x_label] )  if options[:x_label]
  plot.y_label( options[:y_label] )  if options[:y_label]
  p.color( Nyaplot::Colors.qual )    if options[:color]

  plot.show
end