Class: GnuplotRB::Multiplot

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

Overview

Overview

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

Direct Known Subclasses

Animation

Constant Summary

Constants included from OptionHandling

OptionHandling::QUOTED_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Plottable

#method_missing, #own_terminal, #respond_to?, #to_iruby, #to_specific_term

Methods included from OptionHandling

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

Constructor Details

#initialize(*plots, **options) ⇒ Multiplot

Arguments
  • plots are Plot or Splot objects which should be placed on this multiplot layout

  • options will be considered as 'settable' options of gnuplot ('set xrange [1:10]' for { xrange: 1..10 } etc) just as in Plot. Special options of Multiplot are :layout and :title.


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

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)
end

Dynamic Method Handling

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

Instance Attribute Details

#plotsObject (readonly)

Array of plots contained by this object.


10
11
12
# File 'lib/gnuplotrb/multiplot.rb', line 10

def plots
  @plots
end

Instance Method Details

#[](*args) ⇒ Object

Overview

Equal to #plots


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

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

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

Overview

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

Arguments
  • position - position before which you want to add a plot

  • plots - sequence of plots you want to add

Example
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])

104
105
106
107
# File 'lib/gnuplotrb/multiplot.rb', line 104

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

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

Overview

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

Arguments
  • term - Terminal to plot to

  • options - will be considered as 'settable' options of gnuplot ('set xrange [1:10]', 'set title 'plot'' etc)

Options passed here have priority over already existing. Inner options of Plots have the highest priority (except :term and :output which are ignored in this case).


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gnuplotrb/multiplot.rb', line 36

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)) ⇒ Object Also known as: remove

Overview

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

Arguments
  • position - position of plot you want to remove

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

122
123
124
# File 'lib/gnuplotrb/multiplot.rb', line 122

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

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

Overview

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

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

  • plot - replacement for existing plot

Example
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'))

88
89
90
# File 'lib/gnuplotrb/multiplot.rb', line 88

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

#update_plot(position = 0, **options) ⇒ Object Also known as: update

Overview

Create new 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.

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

  • options - options to update plot with

  • *&block* - method also may take a block which returns a plot

Example
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)') }

68
69
70
71
72
73
# File 'lib/gnuplotrb/multiplot.rb', line 68

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