Module: Daru::Plotting::DataFrame

Included in:
DataFrame
Defined in:
lib/daru/plotting/dataframe.rb

Instance Method Summary collapse

Instance Method Details

#plot(opts = {}) {|plot, diagram| ... } ⇒ Object

Plots a DataFrame with Nyaplot on IRuby using the given options. Yields the corresponding Nyaplot::Plot object and the Nyaplot::Diagram object to the block, if it is specified. See the nyaplot docs for info on how to further use these objects.

Detailed instructions on use of the plotting API can be found in the notebooks whose links you can find in the README.

Options

  • :type - Type of plot. Can be :scatter, :bar, :histogram, :line or :box.

  • :x - Vector to be used for X co-ordinates.

  • :y - Vector to be used for Y co-ordinates.

Usage

# Simple bar chart
df = Daru::DataFrame.new({a:['A', 'B', 'C', 'D', 'E'], b:[10,20,30,40,50]})
df.plot type: :bar, x: :a, y: :b

Yields:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/daru/plotting/dataframe.rb', line 22

def plot opts={}
  options = {
    type:  :scatter
  }.merge(opts)

  plot = Nyaplot::Plot.new
  types = extract_option :type, options

  diagram =
    case
    when !([:scatter, :bar, :line, :histogram] & types).empty?
      if single_diagram? options
        add_single_diagram plot, options
      else
        add_multiple_diagrams plot, options
      end
    when types.include?(:box)
      numeric = only_numerics(clone: false).dup_only_valid

      plot.add_with_df(
        numeric.to_nyaplotdf,
        :box, *numeric.vectors.to_a
      )
    end

  yield(plot, diagram) if block_given?

  plot.show
end