Class: GnuplotRB::Plot
- Inherits:
-
Object
- Object
- GnuplotRB::Plot
- Includes:
- Plottable
- Defined in:
- lib/gnuplotrb/plot.rb
Overview
Overview
Plot correspond to simple 2D visualisation
Direct Known Subclasses
Constant Summary
Constants included from OptionHandling
OptionHandling::QUOTED_OPTIONS
Instance Attribute Summary collapse
-
#datasets ⇒ Object
readonly
Array of datasets which are plotted by this object.
Instance Method Summary collapse
-
#[](*args) ⇒ Object
Overview The same as Plot#datasets.
-
#add_datasets(*datasets) ⇒ Object
(also: #add_dataset, #<<)
Overview Create new Plot object where given datasets will be inserted into dataset list before given position (position = 0 by default).
-
#initialize(*datasets) {|_self| ... } ⇒ Plot
constructor
Arguments * datasets are either instances of Dataset class or [data, **dataset_options] arrays from which Dataset may be created * options will be considered as ‘settable’ options of gnuplot (‘set xrange [1:10]’ for { xrange: 1..10 }, “set title ‘plot’” for { title: ‘plot’ } etc).
-
#plot(term = nil, multiplot_part: false, **options) ⇒ Object
(also: #replot)
Overview This outputs plot to term (if given) or to this plot’s own terminal.
-
#remove_dataset(position = -1)) ⇒ Object
Overview Create new Plot object where dataset at given position will be removed from dataset list.
-
#replace_dataset(position = 0, dataset) ⇒ Object
Overview Create new Plot object where dataset at position will be replaced with the given one.
-
#update_dataset(position = 0, data: nil, **options) ⇒ Object
Overview Create new Plot object where dataset at position will be replaced with the new one created from it by updating.
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(*datasets) {|_self| ... } ⇒ Plot
Arguments
-
datasets are either instances of Dataset class or
- data, **dataset_options
-
arrays from which Dataset may be created
-
options will be considered as ‘settable’ options of gnuplot (‘set xrange [1:10]’ for { xrange: 1..10 }, “set title ‘plot’” for { title: ‘plot’ } etc)
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/gnuplotrb/plot.rb', line 17 def initialize(*datasets) # had to relace **options arg with this because in some cases # Daru::DataFrame was mentioned as hash and added to options # instead of plots @options = Hamster.hash if datasets[-1].is_a?(Hamster::Hash) || datasets[-1].is_a?(Hash) @options = Hamster.hash(datasets[-1]) datasets = datasets[0..-2] end @datasets = parse_datasets_array(datasets) @cmd = 'plot ' OptionHandling.(@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
#datasets ⇒ Object (readonly)
Array of datasets which are plotted by this object.
9 10 11 |
# File 'lib/gnuplotrb/plot.rb', line 9 def datasets @datasets end |
Instance Method Details
#[](*args) ⇒ Object
Overview
The same as Plot#datasets
138 139 140 |
# File 'lib/gnuplotrb/plot.rb', line 138 def [](*args) @datasets[*args] end |
#add_datasets(*datasets) ⇒ Object Also known as: add_dataset, <<
Overview
Create new Plot object where given datasets will be inserted into dataset list before given position (position = 0 by default).
Arguments
-
position - position where to insert given datasets
-
datasets - sequence of datasets to add
Example
sinx = Plot.new('sin(x)')
sinx_and_cosx_with_expx = sinx.add(['cos(x)'], ['exp(x)'])
cosx_and_sinx = sinx << ['cos(x)']
111 112 113 114 115 |
# File 'lib/gnuplotrb/plot.rb', line 111 def add_datasets(*datasets) datasets.map! { |ds| ds.is_a?(Numeric) ? ds : dataset_from_any(ds) } datasets.unshift(0) unless datasets[0].is_a?(Numeric) self.class.new(@datasets.insert(*datasets), @options) end |
#plot(term = nil, multiplot_part: false, **options) ⇒ Object Also known as: replot
Overview
This outputs plot to term (if given) or to this plot’s own terminal.
Arguments
-
term - Terminal to plot to
-
multiplot_part - part of a multiplot. Option for inner usage
-
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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/gnuplotrb/plot.rb', line 41 def plot(term = nil, multiplot_part: false, **) fail ArgumentError, 'Empty plots are not supported!' if @datasets.empty? inner_opts = if multiplot_part @options.merge().reject { |key, _| [:term, :output].include?(key) } else @options.merge() end terminal = term || (inner_opts[:output] ? Terminal.new : own_terminal) ds_string = @datasets.map { |dataset| dataset.to_s(terminal) }.join(' , ') full_command = @cmd + ds_string terminal.set(inner_opts).stream_puts(full_command).unset(inner_opts.keys) if inner_opts[: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?(inner_opts[:output]) end self end |
#remove_dataset(position = -1)) ⇒ Object
Overview
Create new Plot object where dataset at given position will be removed from dataset list.
Arguments
-
position - position of dataset that should be removed (by default last dataset is removed)
Example
sinx_and_cosx = Plot.new('sin(x)', 'cos(x)')
sinx = sinx_and_cosx.remove_dataset
cosx = sinx_and_cosx.remove_dataset(0)
131 132 133 |
# File 'lib/gnuplotrb/plot.rb', line 131 def remove_dataset(position = -1) self.class.new(@datasets.delete_at(position), @options) end |
#replace_dataset(position = 0, dataset) ⇒ Object
Overview
Create new Plot object where dataset at position will be replaced with the given one.
Arguments
-
position - position of dataset which you need to update (by default first dataset is replaced)
-
dataset - dataset to replace the old one. You can also give here [data, **dataset_options] array from which Dataset may be created.
Example
sinx = Plot.new('sin(x)')
cosx = sinx.replace_dataset(['cos(x)'])
94 95 96 |
# File 'lib/gnuplotrb/plot.rb', line 94 def replace_dataset(position = 0, dataset) self.class.new(@datasets.set(position, dataset_from_any(dataset)), @options) end |
#update_dataset(position = 0, data: nil, **options) ⇒ Object
Overview
Create new Plot object where dataset at position will be replaced with the new one created from it by updating.
Arguments
-
position - position of dataset which you need to update (by default first dataset is updated)
-
data - data to update dataset with
-
options - options to update dataset with
Example
updated_plot = plot.update_dataset(data: [x1,y1], title: 'After update')
75 76 77 78 79 |
# File 'lib/gnuplotrb/plot.rb', line 75 def update_dataset(position = 0, data: nil, **) old_ds = @datasets[position] new_ds = old_ds.update(data, ) new_ds.equal?(old_ds) ? self : replace_dataset(position, new_ds) end |