Class: GnuplotRB::Multiplot

Inherits:
Object
  • Object
show all
Includes:
Plottable
Defined in:
lib/gnuplotrb/multiplot.rb

Overview

Multiplot allows to place several plots on one layout. It's usage is covered in multiplot notebook.

Options

Most of Multiplot options are the same as in Plot so one can also set any options related to Plot and they will be considered by all nested plots (if they does not override it with their own values).

There are only 2 specific options:

  • title - set title for the whole layout (above all the plots)

  • layout - set layout size, examples:

    { layout : [1, 3] } # 3 plots, 1 row, 3 columns
    { layout : [2, 2] } # 4 plots, 2 rows, 2 columns
    

Direct Known Subclasses

Animation

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Plottable

#method_missing, #option_name, #option_name!, #own_terminal, #respond_to?, #title, #title!, #to_canvas, #to_gif, #to_iruby, #to_png, #to_specific_term, #to_svg, #xrange, #xrange!, #yrange, #yrange!

Methods included from OptionHandling

option_to_string, #options, #options!, ruby_class_to_gnuplot, string_key, valid_terminal?, validate_terminal_options

Constructor Details

#initialize(*plots, **options) {|_self| ... } ⇒ Multiplot

Returns a new instance of Multiplot.

Parameters:

  • plots (Plot, Splot, Hamster::Vector)

    Hamster vector (or just sequence) with Plot or Splot objects which should be placed on this multiplot layout

  • options (Hash)

    see options in top class docs

Yields:

  • (_self)

Yield Parameters:


27
28
29
30
31
32
# File 'lib/gnuplotrb/multiplot.rb', line 27

def initialize(*plots, **options)
  @plots = plots[0].is_a?(Hamster::Vector) ? plots[0] : Hamster::Vector.new(plots)
  @options = Hamster::Hash[options]
  OptionHandling.validate_terminal_options(@options)
  yield(self) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GnuplotRB::Plottable

Instance Attribute Details

#plotsArray (readonly)

Returns Array of plots contained by this object.

Returns:

  • (Array)

    Array of plots contained by this object


21
22
23
# File 'lib/gnuplotrb/multiplot.rb', line 21

def plots
  @plots
end

Instance Method Details

#[](*args) ⇒ Object

Equal to #plots


210
211
212
# File 'lib/gnuplotrb/multiplot.rb', line 210

def [](*args)
  @plots[*args]
end

#add_plots(*plots) ⇒ Multiplot Also known as: add_plot, <<, add

Create new Multiplot with given plots added before plot at given position. (by default it adds plot at the front).

Examples:

mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
enlarged_mp = mp.add_plots(Plot.new('exp(x)')).layout([3,1])# mp IS NOT affected

Parameters:

  • position (Integer)

    position of plot which you need to replace (by default first plot is replace)

  • plots (Sequence of Plot or Splot)

    plots you want to add

Returns:


150
151
152
153
# File 'lib/gnuplotrb/multiplot.rb', line 150

def add_plots(*plots)
  plots.unshift(0) unless plots[0].is_a?(Numeric)
  self.class.new(@plots.insert(*plots), @options)
end

#add_plots!(*plots) ⇒ Multiplot Also known as: add_plot!, add!

Destructive version of #add_plots.

Examples:

mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
mp.add_plots!(Plot.new('exp(x)')).layout([3,1])# mp IS affected

Returns:


167
168
169
170
171
# File 'lib/gnuplotrb/multiplot.rb', line 167

def add_plots!(*plots)
  plots.unshift(0) unless plots[0].is_a?(Numeric)
  @plots = @plots.insert(*plots)
  self
end

#plot(term = nil, multiplot_part: false, **options) ⇒ Multiplot

Output all the plots to term (if given) or to this Multiplot's own terminal.

Parameters:

  • term (Terminal) (defaults to: nil)

    Terminal to plot to

  • multiplot_part (Boolean) (defaults to: false)

    placeholder, does not really needed and should not be used

  • options (Hash)

    see options in top class docs. Options passed here have priority over already set.

Returns:


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gnuplotrb/multiplot.rb', line 42

def plot(term = nil, multiplot_part: false, **options)
  plot_options = mix_options(options) do |plot_opts, mp_opts|
    plot_opts.merge(multiplot: mp_opts.to_h)
  end
  terminal = term || (plot_options[:output] ? Terminal.new : own_terminal)
  multiplot(terminal, plot_options)
  if plot_options[:output]    # guaranteed wait for plotting to finish

    terminal.close unless term    # not guaranteed wait for plotting to finish
    # work bad with terminals like svg and html

    sleep 0.01 until File.size?(plot_options[:output])
  end
  self
end

#remove_plot(position = -1)) ⇒ Multiplot Also known as: remove

Create new Multiplot without plot at given position (by default last plot is removed).

Examples:

mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
mp_with_only_cos = mp.remove_plot(0)# mp IS NOT affected

Parameters:

  • position (Integer) (defaults to: -1))

    position of plot which you need to remove (by default last plot is removed)

Returns:


187
188
189
# File 'lib/gnuplotrb/multiplot.rb', line 187

def remove_plot(position = -1)
  self.class.new(@plots.delete_at(position), @options)
end

#remove_plot!(position = -1)) ⇒ Multiplot Also known as: remove!

Destructive version of #remove_plot.

Examples:

mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
mp.remove_plot!(0)# mp IS affected

Returns:


201
202
203
204
# File 'lib/gnuplotrb/multiplot.rb', line 201

def remove_plot!(position = -1)
  @plots = @plots.delete_at(position)
  self
end

#replace_plot(position = 0, plot) ⇒ Multiplot Also known as: replace

Create new Multiplot object where plot (Plot or Splot object) at position will be replaced with the given one.

Examples:

mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
mp_with_replaced_plot = mp.replace_plot(Plot.new('exp(x)', title: 'exp instead of sin'))# mp IS NOT affected

Parameters:

  • position (Integer) (defaults to: 0)

    position of plot which you need to replace (by default first plot is replace)

  • plot (Plot, Splot)

    replacement

Returns:


116
117
118
# File 'lib/gnuplotrb/multiplot.rb', line 116

def replace_plot(position = 0, plot)
  self.class.new(@plots.set(position, plot), @options)
end

#replace_plot!(position = 0, plot) ⇒ Multiplot Also known as: replace!, []=

Destructive version of #replace_plot.

Examples:

mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
mp.replace_plot!(Plot.new('exp(x)', title: 'exp instead of sin'))# mp IS affected

Returns:


130
131
132
133
# File 'lib/gnuplotrb/multiplot.rb', line 130

def replace_plot!(position = 0, plot)
  @plots = @plots.set(position, plot)
  self
end

#update_plot(position = 0, **options) {|plot| ... } ⇒ Multiplot Also known as: update

Create new updated Multiplot object where plot (Plot or Splot object) at position will be replaced with the new one created from it by updating. To update a plot you can pass some options for it or a block, that should take existing plot (with new options if you gave them) and return a plot too.

Method yields new created Plot or Splot to allow you update it manually.

Examples:

mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
updated_mp = mp.update_plot(title: 'Sin(x) and Exp(x)') { |sinx| sinx.add!('exp(x)') }# mp IS NOT affected

Parameters:

  • position (Integer) (defaults to: 0)

    position of plot which you need to update (by default first plot is updated)

  • options (Hash)

    options to set into updated plot

Yield Parameters:

Yield Returns:

Returns:


78
79
80
81
82
83
# File 'lib/gnuplotrb/multiplot.rb', line 78

def update_plot(position = 0, **options)
  return self unless block_given? if options.empty?
  replacement = @plots[position].options(options)
  replacement = yield(replacement) if block_given?
  replace_plot(position, replacement)
end

#update_plot!(position = 0, **options) {|replacement| ... } ⇒ Multiplot Also known as: update!

Destructive version of #update_plot.

Examples:

Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
mp.update_plot!(title: 'Sin(x) and Exp(x)') { |sinx| sinx.add!('exp(x)') }# mp IS affected

Yields:

  • (replacement)

Returns:


95
96
97
98
99
100
# File 'lib/gnuplotrb/multiplot.rb', line 95

def update_plot!(position = 0, **options)
  return self unless block_given? if options.empty?
  replacement = @plots[position].options!(options)
  yield(replacement) if block_given?
  self
end