Class: SPCore::Plotter
- Inherits:
-
Object
- Object
- SPCore::Plotter
- Includes:
- Hashmake::HashMakeable
- Defined in:
- lib/spcore/util/plotter.rb
Overview
Helps make plotting data even easier. Uses gnuplot.
Constant Summary collapse
- ARG_SPECS =
Used to process hashed args passed to #initialize.
{ :title => arg_spec(:type => String, :reqd => false, :default => ""), :xlabel => arg_spec(:type => String, :reqd => false, :default => "x"), :ylabel => arg_spec(:type => String, :reqd => false, :default => "y"), :linestyle => arg_spec(:type => String, :reqd => false, :default => "lines"), :linewidth => arg_spec(:type => Fixnum, :reqd => false, :default => 1, :validator => ->(a){ a >= 1 }), :logscale => arg_spec(:type => String, :reqd => false, :default => ""), }
Class Method Summary collapse
-
.plot_1d(titled_sequences) ⇒ Object
Plot a sequence of values.
-
.plot_2d(titled_hashes) ⇒ Object
Plot XY datapoints.
-
.plot_datasets(datasets) ⇒ Object
Plot Gnuplot::DataSet objects.
-
.plot_signals(signals_hash) ⇒ Object
Plot data from Signal objects.
Instance Method Summary collapse
-
#initialize(hashed_args = {}) ⇒ Plotter
constructor
A new instance of Plotter.
-
#plot_1d(titled_sequences) ⇒ Object
Plot a sequence of values.
-
#plot_2d(titled_hashes) ⇒ Object
Plot XY datapoints.
-
#plot_datasets(datasets) ⇒ Object
Plot Gnuplot::DataSet objects.
-
#plot_signals(signals_hash) ⇒ Object
Plot data from Signal objects.
Constructor Details
Class Method Details
.plot_1d(titled_sequences) ⇒ Object
Plot a sequence of values.
104 105 106 |
# File 'lib/spcore/util/plotter.rb', line 104 def self.plot_1d titled_sequences return Plotter.new.plot_1d titled_sequences end |
.plot_2d(titled_hashes) ⇒ Object
Plot XY datapoints.
62 63 64 |
# File 'lib/spcore/util/plotter.rb', line 62 def self.plot_2d titled_hashes return Plotter.new.plot_2d titled_hashes end |
Instance Method Details
#plot_1d(titled_sequences) ⇒ Object
Plot a sequence of values.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/spcore/util/plotter.rb', line 74 def plot_1d titled_sequences datasets = [] titled_sequences.each do |title, sequence| indices = Array.new(sequence.size) sequence.each_index do |i| indices[i] = i #if plot_against_fraction # indices[i] /= sequence.size.to_f #end end dataset = Gnuplot::DataSet.new( [indices, sequence] ){ |ds| ds.with = @linestyle ds.title = title ds.linewidth = @linewidth } datasets << dataset end plot_datasets datasets end |
#plot_2d(titled_hashes) ⇒ Object
Plot XY datapoints.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/spcore/util/plotter.rb', line 34 def plot_2d titled_hashes datasets = [] titled_hashes.each do |title, hash| # sort the data sorted = {} hash.keys.sort.each do |key| sorted[key] = hash[key] end dataset = Gnuplot::DataSet.new( [sorted.keys, sorted.values] ){ |ds| ds.with = @linestyle ds.title = title ds.linewidth = @linewidth } datasets << dataset end plot_datasets datasets end |
#plot_datasets(datasets) ⇒ Object
Plot Gnuplot::DataSet objects.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/spcore/util/plotter.rb', line 110 def plot_datasets datasets Gnuplot.open do |gp| Gnuplot::Plot.new(gp) do |plot| plot.title @title plot.xlabel @xlabel plot.ylabel @ylabel plot.data = datasets if @logscale_x plot.logscale "x" end end end end |
#plot_signals(signals_hash) ⇒ Object
Plot data from Signal objects.
133 134 135 136 137 138 139 |
# File 'lib/spcore/util/plotter.rb', line 133 def plot_signals signals_hash data_hash = {} signals_hash.each do |name, signal| data_hash[name] = signal.data end plot_1d data_hash end |