gnuplotrb 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +66 -3
- data/Rakefile +3 -4
- data/gnuplotrb.gemspec +1 -1
- data/lib/gnuplotrb.rb +5 -0
- data/lib/gnuplotrb/animation.rb +36 -9
- data/lib/gnuplotrb/external_classes/array.rb +1 -0
- data/lib/gnuplotrb/external_classes/daru.rb +8 -0
- data/lib/gnuplotrb/external_classes/string.rb +2 -3
- data/lib/gnuplotrb/fit.rb +188 -160
- data/lib/gnuplotrb/mixins/error_handling.rb +6 -4
- data/lib/gnuplotrb/mixins/option_handling.rb +60 -34
- data/lib/gnuplotrb/mixins/plottable.rb +109 -26
- data/lib/gnuplotrb/multiplot.rb +123 -44
- data/lib/gnuplotrb/plot.rb +145 -42
- data/lib/gnuplotrb/splot.rb +8 -8
- data/lib/gnuplotrb/staff/datablock.rb +54 -12
- data/lib/gnuplotrb/staff/dataset.rb +118 -48
- data/lib/gnuplotrb/staff/settings.rb +24 -21
- data/lib/gnuplotrb/staff/terminal.rb +68 -34
- data/lib/gnuplotrb/version.rb +3 -1
- metadata +7 -6
@@ -9,10 +9,9 @@ module GnuplotRB
|
|
9
9
|
# handle errors from its stderr.
|
10
10
|
module ErrorHandling
|
11
11
|
##
|
12
|
-
# ====== Overview
|
13
12
|
# Check if there were errors in previous commands.
|
14
13
|
# Throws GnuplotError in case of any errors.
|
15
|
-
def check_errors
|
14
|
+
def check_errors(raw: false)
|
16
15
|
return if @err_array.empty?
|
17
16
|
command = ''
|
18
17
|
rest = ''
|
@@ -21,14 +20,17 @@ module GnuplotRB
|
|
21
20
|
rest = @err_array[1..-1].join('; ')
|
22
21
|
@err_array.clear
|
23
22
|
end
|
24
|
-
message =
|
23
|
+
message = if raw
|
24
|
+
"#{command};#{rest}}"
|
25
|
+
else
|
26
|
+
"Error in previous command (\"#{command}\"): \"#{rest}\""
|
27
|
+
end
|
25
28
|
fail GnuplotError, message
|
26
29
|
end
|
27
30
|
|
28
31
|
private
|
29
32
|
|
30
33
|
##
|
31
|
-
# ====== Overview
|
32
34
|
# Start new thread that will read stderr given as stream
|
33
35
|
# and add errors into @err_array.
|
34
36
|
def handle_stderr(stream)
|
@@ -1,13 +1,12 @@
|
|
1
1
|
module GnuplotRB
|
2
2
|
##
|
3
|
-
# ====== Overview
|
4
3
|
# This module contains methods which are mixed into several classes
|
5
4
|
# to set, get and convert their options.
|
6
5
|
module OptionHandling
|
7
6
|
class << self
|
8
|
-
# Some values of options should be quoted to be read by gnuplot
|
7
|
+
# Some values of options should be quoted to be read by gnuplot properly
|
9
8
|
#
|
10
|
-
#
|
9
|
+
# @todo update list with data from gnuplot documentation !!!
|
11
10
|
QUOTED_OPTIONS = %w(
|
12
11
|
title
|
13
12
|
output
|
@@ -30,26 +29,33 @@ module GnuplotRB
|
|
30
29
|
format_z
|
31
30
|
format_cb
|
32
31
|
timefmt
|
32
|
+
dt
|
33
|
+
dashtype
|
33
34
|
)
|
34
35
|
|
36
|
+
private_constant :QUOTED_OPTIONS
|
37
|
+
|
35
38
|
##
|
36
|
-
#
|
37
|
-
# with the same first word of key.
|
38
|
-
#
|
39
|
+
# Replace '_' with ' ' is made to allow passing several options
|
40
|
+
# with the same first word of key. See issue #7 for more info.
|
41
|
+
# @param key [Symbol, String] key to modify
|
42
|
+
# @return [String] given key with '_' replaced with ' '
|
39
43
|
def string_key(key)
|
40
44
|
key.to_s.gsub(/_/) { ' ' } + ' '
|
41
45
|
end
|
42
46
|
|
43
47
|
##
|
44
|
-
# ====== Overview
|
45
48
|
# Recursive function that converts Ruby option to gnuplot string
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
49
|
-
#
|
50
|
-
# option_to_string(['png', size: [300, 300]])
|
51
|
-
#
|
52
|
-
# option_to_string(
|
49
|
+
#
|
50
|
+
# @param key [Symbol] name of option in gnuplot
|
51
|
+
# @param option an option that should be converted
|
52
|
+
# @example
|
53
|
+
# option_to_string(['png', size: [300, 300]])
|
54
|
+
# #=> 'png size 300,300'
|
55
|
+
# option_to_string(xrange: 0..100)
|
56
|
+
# #=> 'xrange [0:100]'
|
57
|
+
# option_to_string(multiplot: true)
|
58
|
+
# #=> 'multiplot'
|
53
59
|
def option_to_string(key = nil, option)
|
54
60
|
return string_key(key) if !!option == option # check for boolean
|
55
61
|
value = ruby_class_to_gnuplot(option)
|
@@ -60,6 +66,7 @@ module GnuplotRB
|
|
60
66
|
end
|
61
67
|
|
62
68
|
##
|
69
|
+
# @private
|
63
70
|
# Method for inner use.
|
64
71
|
# Needed to convert several ruby classes into
|
65
72
|
# value that should be piped to gnuplot.
|
@@ -79,35 +86,33 @@ module GnuplotRB
|
|
79
86
|
end
|
80
87
|
|
81
88
|
##
|
82
|
-
# ====== Overview
|
83
89
|
# Check if given terminal available for use.
|
84
|
-
#
|
85
|
-
#
|
90
|
+
#
|
91
|
+
# @param terminal [String] terminal to check (e.g. 'png', 'qt', 'gif')
|
92
|
+
# @return [Boolean] true or false
|
86
93
|
def valid_terminal?(terminal)
|
87
94
|
Settings.available_terminals.include?(terminal)
|
88
95
|
end
|
89
96
|
|
90
97
|
##
|
91
|
-
# ====== Overview
|
92
98
|
# Check if given options are valid for gnuplot.
|
93
99
|
# Raises ArgumentError if invalid options found.
|
94
|
-
# ====== Arguments
|
95
|
-
# * *options* - Hash of options to check
|
96
|
-
# (e.g. {term: 'qt', title: 'Plot title'})
|
97
|
-
#
|
98
100
|
# Now checks only terminal name.
|
101
|
+
#
|
102
|
+
# @param options [Hash] options to check (e.g. "{ term: 'qt', title: 'Plot title' }")
|
99
103
|
def validate_terminal_options(options)
|
100
104
|
terminal = options[:term]
|
101
105
|
return unless terminal
|
102
106
|
terminal = terminal[0] if terminal.is_a?(Array)
|
103
107
|
message = 'Seems like your Gnuplot does not ' \
|
104
|
-
|
105
|
-
'terminals with Settings::available_terminals'
|
108
|
+
"support that terminal (#{terminal}), please see " \
|
109
|
+
'supported terminals with Settings::available_terminals'
|
106
110
|
fail(ArgumentError, message) unless valid_terminal?(terminal)
|
107
111
|
end
|
108
112
|
end
|
109
113
|
|
110
114
|
##
|
115
|
+
# @private
|
111
116
|
# You should implement #initialize in classes that use OptionsHelper
|
112
117
|
def initialize(*_)
|
113
118
|
fail NotImplementedError, 'You should implement #initialize' \
|
@@ -115,6 +120,7 @@ module GnuplotRB
|
|
115
120
|
end
|
116
121
|
|
117
122
|
##
|
123
|
+
# @private
|
118
124
|
# You should implement #new_with_options in classes that use OptionsHelper
|
119
125
|
def new_with_options(*_)
|
120
126
|
fail NotImplementedError, 'You should implement #new_with_options' \
|
@@ -122,20 +128,19 @@ module GnuplotRB
|
|
122
128
|
end
|
123
129
|
|
124
130
|
##
|
125
|
-
# ====== Overview
|
126
131
|
# Create new Plot (or Dataset or Splot or Multiplot) object where current
|
127
132
|
# options are merged with given. If no options
|
128
133
|
# given it will just return existing set of options.
|
129
|
-
#
|
130
|
-
#
|
131
|
-
#
|
132
|
-
#
|
134
|
+
#
|
135
|
+
# @param options [Hash] options to add
|
136
|
+
# @return [Dataset, Splot, Multiplot] new object created with given options
|
137
|
+
# @return [Hamster::Hash] current options if given options empty
|
138
|
+
# @example
|
133
139
|
# sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
|
134
140
|
# sin_graph.plot
|
135
141
|
# sin_graph_update = sin_graph.options(title: 'Sin on [-10:10]', xrange: -10..10)
|
136
142
|
# sin_graph_update.plot
|
137
|
-
# #
|
138
|
-
# # sin_graph.title(...).xrange(...)
|
143
|
+
# # sin_graph IS NOT affected
|
139
144
|
def options(**options)
|
140
145
|
@options ||= Hamster::Hash.new
|
141
146
|
if options.empty?
|
@@ -145,12 +150,27 @@ module GnuplotRB
|
|
145
150
|
end
|
146
151
|
end
|
147
152
|
|
153
|
+
##
|
154
|
+
# Update existing Plot (or Dataset or Splot or Multiplot) object with given options.
|
155
|
+
#
|
156
|
+
# @param options [Hash] options to add
|
157
|
+
# @return [Dataset, Splot, Multiplot] self
|
158
|
+
# @example
|
159
|
+
# sin_graph = Plot.new(['sin(x)', title: 'Sin'], title: 'Sin on [0:3]', xrange: 0..3)
|
160
|
+
# sin_graph.plot
|
161
|
+
# sin_graph.options!(title: 'Sin on [-10:10]', xrange: -10..10)
|
162
|
+
# sin_graph.plot
|
163
|
+
# # second #plot call will plot not the same as first, sin_graph IS affected
|
164
|
+
def options!(**options)
|
165
|
+
@options = @options ? @options.merge(options) : Hamster::Hash.new(options)
|
166
|
+
self
|
167
|
+
end
|
168
|
+
|
148
169
|
private
|
149
170
|
|
150
171
|
##
|
151
|
-
#
|
152
|
-
#
|
153
|
-
# * *value* - anything treated as options value in gnuplot gem
|
172
|
+
# Return current option value if no value given. Create new
|
173
|
+
# object with given option set if value given.
|
154
174
|
def option(key, *value)
|
155
175
|
if value.empty?
|
156
176
|
value = options[key]
|
@@ -160,5 +180,11 @@ module GnuplotRB
|
|
160
180
|
options(key => value)
|
161
181
|
end
|
162
182
|
end
|
183
|
+
|
184
|
+
##
|
185
|
+
# Just set an option.
|
186
|
+
def option!(key, *value)
|
187
|
+
options!(key => value)
|
188
|
+
end
|
163
189
|
end
|
164
190
|
end
|
@@ -7,57 +7,64 @@ module GnuplotRB
|
|
7
7
|
include OptionHandling
|
8
8
|
|
9
9
|
##
|
10
|
+
# @private
|
10
11
|
# You should implement #plot in classes that are Plottable
|
11
12
|
def plot(*_)
|
12
13
|
fail NotImplementedError, 'You should implement #plot in classes that are Plottable!'
|
13
14
|
end
|
14
15
|
|
15
16
|
##
|
16
|
-
# ====== Overview
|
17
17
|
# In this gem #method_missing is used both to handle
|
18
18
|
# options and to handle plotting to specific terminal.
|
19
19
|
#
|
20
|
-
#
|
21
|
-
#
|
20
|
+
# == Options handling
|
21
|
+
# === Overview
|
22
22
|
# You may set options using #option_name(option_value) method.
|
23
23
|
# A new object will be constructed with selected option set.
|
24
24
|
# And finally you can get current value of any option using
|
25
25
|
# #options_name without arguments.
|
26
|
-
#
|
26
|
+
# === Arguments
|
27
27
|
# * *option_value* - value to set an option. If none given
|
28
28
|
# method will just return current option's value
|
29
|
-
#
|
29
|
+
# === Examples
|
30
30
|
# plot = Splot.new
|
31
31
|
# new_plot = plot.title('Awesome plot')
|
32
32
|
# plot.title #=> nil
|
33
33
|
# new_plot.title #=> 'Awesome plot'
|
34
34
|
#
|
35
|
-
#
|
36
|
-
#
|
35
|
+
# == Plotting to specific term
|
36
|
+
# === Overview
|
37
37
|
# Gnuplot offers possibility to output graphics to many image formats.
|
38
38
|
# The easiest way to to so is to use #to_<plot_name> methods.
|
39
|
-
#
|
39
|
+
# === Arguments
|
40
40
|
# * *options* - set of options related to terminal (size, font etc).
|
41
41
|
# Be careful, some terminals have their own specific options.
|
42
|
-
#
|
42
|
+
# === Examples
|
43
43
|
# # font options specific for png term
|
44
44
|
# multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
|
45
45
|
# # font options specific for svg term
|
46
46
|
# content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)
|
47
47
|
def method_missing(meth_id, *args)
|
48
48
|
meth = meth_id.id2name
|
49
|
-
|
49
|
+
case
|
50
|
+
when meth[0..2] == 'to_'
|
50
51
|
term = meth[3..-1]
|
51
52
|
super unless OptionHandling.valid_terminal?(term)
|
52
53
|
to_specific_term(term, *args)
|
54
|
+
when meth[-1] == '!'
|
55
|
+
option!(meth[0..-2].to_sym, *args)
|
56
|
+
when meth[-1] == '='
|
57
|
+
option!(meth[0..-2].to_sym, *args)
|
58
|
+
option(meth[0..-2].to_sym)
|
53
59
|
else
|
54
60
|
option(meth_id, *args)
|
55
61
|
end
|
56
62
|
end
|
57
63
|
|
58
64
|
##
|
59
|
-
#
|
60
|
-
#
|
65
|
+
# @return [true] for existing methods and
|
66
|
+
# #to_|term_name| when name is a valid terminal type.
|
67
|
+
# @return [false] otherwise
|
61
68
|
def respond_to?(meth_id)
|
62
69
|
# Next line is here to force iRuby use #to_iruby
|
63
70
|
# instead of #to_svg.
|
@@ -68,8 +75,7 @@ module GnuplotRB
|
|
68
75
|
end
|
69
76
|
|
70
77
|
##
|
71
|
-
# This method is used to embed plottable objects
|
72
|
-
# into iRuby notebooks. There is
|
78
|
+
# This method is used to embed plottable objects into iRuby notebooks. There is
|
73
79
|
# {a notebook}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/basic_usage.ipynb]
|
74
80
|
# with examples of its usage.
|
75
81
|
def to_iruby
|
@@ -86,20 +92,16 @@ module GnuplotRB
|
|
86
92
|
end
|
87
93
|
|
88
94
|
##
|
89
|
-
#
|
90
|
-
#
|
91
|
-
# to_svg -> svg file contents
|
92
|
-
# to_canvas('plot.html', size: [300,300]) -> creates file with plot
|
93
|
-
#
|
94
|
-
# ====== Overview
|
95
|
-
# Method which outputs plot to specific terminal (possibly some file).
|
95
|
+
# @private
|
96
|
+
# Output plot to specific terminal (possibly some file).
|
96
97
|
# Explicit use should be avoided. This method is called from #method_missing
|
97
98
|
# when it handles method names like #to_png(options).
|
98
|
-
#
|
99
|
-
#
|
99
|
+
#
|
100
|
+
# @param trminal [String] terminal name ('png', 'svg' etc)
|
101
|
+
# @param path [String] path to output file, if none given it will output to temp file
|
100
102
|
# and then read it and return binary contents of file
|
101
|
-
#
|
102
|
-
#
|
103
|
+
# @param options [Hash] used in #plot
|
104
|
+
# @example
|
103
105
|
# ## plot here may be Plot, Splot, Multiplot or any other plottable class
|
104
106
|
# plot.to_png('./result.png', size: [300, 500])
|
105
107
|
# contents = plot.to_svg(size: [100, 100])
|
@@ -117,9 +119,90 @@ module GnuplotRB
|
|
117
119
|
end
|
118
120
|
|
119
121
|
##
|
120
|
-
#
|
122
|
+
# @return [Terminal] terminal object linked with this Plottable object
|
121
123
|
def own_terminal
|
122
124
|
@terminal ||= Terminal.new
|
123
125
|
end
|
126
|
+
|
127
|
+
##
|
128
|
+
# @!method xrange(value = nil)
|
129
|
+
# @!method yrange(value = nil)
|
130
|
+
# @!method title(value = nil)
|
131
|
+
# @!method option_name(value = nil)
|
132
|
+
# Clone existing object and set new options value in created one or just return
|
133
|
+
# existing value if nil given.
|
134
|
+
#
|
135
|
+
# Method is handled by #method_missing.
|
136
|
+
#
|
137
|
+
# You may set options using #option_name(option_value) method.
|
138
|
+
# A new object will be constructed with selected option set.
|
139
|
+
# And finally you can get current value of any option using
|
140
|
+
# #options_name without arguments.
|
141
|
+
#
|
142
|
+
# Available options are listed in Plot, Splot, Multiplot etc class top level doc.
|
143
|
+
#
|
144
|
+
# @param value new value for option
|
145
|
+
# @return new object with option_name set to *value* if value given
|
146
|
+
# @return old option value if no value given
|
147
|
+
#
|
148
|
+
# @example
|
149
|
+
# plot = Splot.new
|
150
|
+
# new_plot = plot.title('Awesome plot')
|
151
|
+
# plot.title #=> nil
|
152
|
+
# new_plot.title #=> 'Awesome plot'
|
153
|
+
|
154
|
+
##
|
155
|
+
# @!method xrange!(value)
|
156
|
+
# @!method yrange!(value)
|
157
|
+
# @!method title!(value)
|
158
|
+
# @!method option_name!(value)
|
159
|
+
# Set value for an option.
|
160
|
+
#
|
161
|
+
# Method is handled by #method_missing.
|
162
|
+
#
|
163
|
+
# You may set options using obj.option_name!(option_value) or
|
164
|
+
# obj.option_name = option_value methods.
|
165
|
+
#
|
166
|
+
# Available options are listed in Plot, Splot, Multiplot etc class top level doc.
|
167
|
+
#
|
168
|
+
# @param value new value for option
|
169
|
+
# @return self
|
170
|
+
#
|
171
|
+
# @example
|
172
|
+
# plot = Splot.new
|
173
|
+
# plot.title #=> nil
|
174
|
+
# plot.title!('Awesome plot')
|
175
|
+
# plot.title #=> 'Awesome plot'
|
176
|
+
#
|
177
|
+
# @example
|
178
|
+
# plot = Splot.new
|
179
|
+
# plot.title #=> nil
|
180
|
+
# plot.title = 'Awesome plot'
|
181
|
+
# plot.title #=> 'Awesome plot'
|
182
|
+
|
183
|
+
##
|
184
|
+
# @!method to_png(path = nil, **options)
|
185
|
+
# @!method to_svg(path = nil, **options)
|
186
|
+
# @!method to_gif(path = nil, **options)
|
187
|
+
# @!method to_canvas(path = nil, **options)
|
188
|
+
# Output to plot to according image format.
|
189
|
+
#
|
190
|
+
# All of #to_|terminal_name| methods are handled with #method_missing.
|
191
|
+
#
|
192
|
+
# Gnuplot offers possibility to output graphics to many image formats.
|
193
|
+
# The easiest way to to so is to use #to_<plot_name> methods.
|
194
|
+
#
|
195
|
+
# @param path [String] path to save plot file to.
|
196
|
+
# @param options [Hash] specific terminal options like 'size',
|
197
|
+
# 'font' etc
|
198
|
+
#
|
199
|
+
# @return [String] contents of plotted file unless path given
|
200
|
+
# @return self if path given
|
201
|
+
#
|
202
|
+
# @example
|
203
|
+
# # font options specific for png term
|
204
|
+
# multiplot.to_png('./result.png', size: [300, 500], font: ['arial', 12])
|
205
|
+
# # font options specific for svg term
|
206
|
+
# content = multiplot.to_svg(size: [100, 100], fname: 'Arial', fsize: 12)
|
124
207
|
end
|
125
208
|
end
|
data/lib/gnuplotrb/multiplot.rb
CHANGED
@@ -1,38 +1,44 @@
|
|
1
1
|
module GnuplotRB
|
2
2
|
##
|
3
|
-
# === Overview
|
4
3
|
# Multiplot allows to place several plots on one layout.
|
5
|
-
# It's usage is covered in
|
4
|
+
# It's usage is covered in
|
5
|
+
# {multiplot notebook}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/multiplot_layout.ipynb].
|
6
|
+
#
|
7
|
+
# == Options
|
8
|
+
# Most of Multiplot options are the same as in Plot so one can also set any options related
|
9
|
+
# to Plot and they will be considered by all nested plots
|
10
|
+
# (if they does not override it with their own values).
|
11
|
+
#
|
12
|
+
# There are only 2 specific options:
|
13
|
+
# * title - set title for the whole layout (above all the plots)
|
14
|
+
# * layout - set layout size, examples:
|
15
|
+
# { layout : [1, 3] } # 3 plots, 1 row, 3 columns
|
16
|
+
# { layout : [2, 2] } # 4 plots, 2 rows, 2 columns
|
6
17
|
class Multiplot
|
7
18
|
include Plottable
|
8
19
|
##
|
9
|
-
# Array of plots contained by this object
|
20
|
+
# @return [Array] Array of plots contained by this object
|
10
21
|
attr_reader :plots
|
11
22
|
|
12
23
|
##
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
# * *options* will be considered as 'settable' options of gnuplot
|
17
|
-
# ('set xrange [1:10]' for { xrange: 1..10 } etc) just as in Plot.
|
18
|
-
# Special options of Multiplot are :layout and :title.
|
24
|
+
# @param plots [Plot, Splot, Hamster::Vector] Hamster vector (or just sequence) with Plot
|
25
|
+
# or Splot objects which should be placed on this multiplot layout
|
26
|
+
# @param options [Hash] see options in top class docs
|
19
27
|
def initialize(*plots, **options)
|
20
28
|
@plots = plots[0].is_a?(Hamster::Vector) ? plots[0] : Hamster::Vector.new(plots)
|
21
29
|
@options = Hamster.hash(options)
|
22
30
|
OptionHandling.validate_terminal_options(@options)
|
31
|
+
yield(self) if block_given?
|
23
32
|
end
|
24
33
|
|
25
34
|
##
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
# Options passed here have priority over already existing.
|
34
|
-
# Inner options of Plots have the highest priority (except
|
35
|
-
# :term and :output which are ignored in this case).
|
35
|
+
# Output all the plots to term (if given) or to this Multiplot's own terminal.
|
36
|
+
#
|
37
|
+
# @param term [Terminal] Terminal to plot to
|
38
|
+
# @param multiplot_part [Boolean] placeholder, does not really needed and should not be used
|
39
|
+
# @param options [Hash] see options in top class docs.
|
40
|
+
# Options passed here have priority over already set.
|
41
|
+
# @return [Multiplot] self
|
36
42
|
def plot(term = nil, multiplot_part: false, **options)
|
37
43
|
plot_options = mix_options(options) do |plot_opts, mp_opts|
|
38
44
|
plot_opts.merge(multiplot: mp_opts.to_h)
|
@@ -50,21 +56,25 @@ module GnuplotRB
|
|
50
56
|
end
|
51
57
|
|
52
58
|
##
|
53
|
-
#
|
54
|
-
#
|
55
|
-
# at *position* will
|
59
|
+
# Create new updated Multiplot object
|
60
|
+
# where plot (Plot or Splot object) at *position* will
|
56
61
|
# be replaced with the new one created from it by updating.
|
57
62
|
# To update a plot you can pass some options for it or a
|
58
63
|
# block, that should take existing plot (with new options if
|
59
64
|
# you gave them) and return a plot too.
|
60
|
-
#
|
61
|
-
#
|
65
|
+
#
|
66
|
+
# Method yields new created Plot or Splot to allow you update it manually.
|
67
|
+
#
|
68
|
+
# @param position [Integer] position of plot which you need to update
|
62
69
|
# (by default first plot is updated)
|
63
|
-
#
|
64
|
-
#
|
65
|
-
#
|
70
|
+
# @param options [Hash] options to set into updated plot
|
71
|
+
# @return [Multiplot] self
|
72
|
+
# @yieldparam plot [Plot, Splot] a new plot
|
73
|
+
# @yieldreturn [Plot, Splot] changed plot
|
74
|
+
# @example
|
66
75
|
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
|
67
|
-
# updated_mp = mp.update_plot(title: 'Sin(x) and Exp(x)') { |sinx| sinx.add('exp(x)') }
|
76
|
+
# updated_mp = mp.update_plot(title: 'Sin(x) and Exp(x)') { |sinx| sinx.add!('exp(x)') }
|
77
|
+
# # mp IS NOT affected
|
68
78
|
def update_plot(position = 0, **options)
|
69
79
|
return self unless block_given? if options.empty?
|
70
80
|
replacement = @plots[position].options(options)
|
@@ -75,16 +85,34 @@ module GnuplotRB
|
|
75
85
|
alias_method :update, :update_plot
|
76
86
|
|
77
87
|
##
|
78
|
-
#
|
88
|
+
# Destructive version of #update_plot.
|
89
|
+
#
|
90
|
+
# @return [Multiplot] self
|
91
|
+
# @example
|
92
|
+
# Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
|
93
|
+
# mp.update_plot!(title: 'Sin(x) and Exp(x)') { |sinx| sinx.add!('exp(x)') }
|
94
|
+
# # mp IS affected
|
95
|
+
def update_plot!(position = 0, **options)
|
96
|
+
return self unless block_given? if options.empty?
|
97
|
+
replacement = @plots[position].options!(options)
|
98
|
+
yield(replacement) if block_given?
|
99
|
+
self
|
100
|
+
end
|
101
|
+
|
102
|
+
alias_method :update!, :update_plot!
|
103
|
+
|
104
|
+
##
|
79
105
|
# Create new Multiplot object where plot (Plot or Splot object)
|
80
106
|
# at *position* will be replaced with the given one.
|
81
|
-
#
|
82
|
-
#
|
83
|
-
# (by default first plot is
|
84
|
-
#
|
85
|
-
#
|
107
|
+
#
|
108
|
+
# @param position [Integer] position of plot which you need to replace
|
109
|
+
# (by default first plot is replace)
|
110
|
+
# @param plot [Plot, Splot] replacement
|
111
|
+
# @return [Multiplot] self
|
112
|
+
# @example
|
86
113
|
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
|
87
114
|
# mp_with_replaced_plot = mp.replace_plot(Plot.new('exp(x)', title: 'exp instead of sin'))
|
115
|
+
# # mp IS NOT affected
|
88
116
|
def replace_plot(position = 0, plot)
|
89
117
|
self.class.new(@plots.set(position, plot), @options)
|
90
118
|
end
|
@@ -92,15 +120,33 @@ module GnuplotRB
|
|
92
120
|
alias_method :replace, :replace_plot
|
93
121
|
|
94
122
|
##
|
95
|
-
#
|
123
|
+
# Destructive version of #replace_plot.
|
124
|
+
#
|
125
|
+
# @return [Multiplot] self
|
126
|
+
# @example
|
127
|
+
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
|
128
|
+
# mp.replace_plot!(Plot.new('exp(x)', title: 'exp instead of sin'))
|
129
|
+
# # mp IS affected
|
130
|
+
def replace_plot!(position = 0, plot)
|
131
|
+
@plots = @plots.set(position, plot)
|
132
|
+
self
|
133
|
+
end
|
134
|
+
|
135
|
+
alias_method :replace!, :replace_plot!
|
136
|
+
alias_method :[]=, :replace_plot!
|
137
|
+
|
138
|
+
##
|
96
139
|
# Create new Multiplot with given *plots* added before plot at given *position*.
|
97
140
|
# (by default it adds plot at the front).
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
141
|
+
#
|
142
|
+
# @param position [Integer] position of plot which you need to replace
|
143
|
+
# (by default first plot is replace)
|
144
|
+
# @param plots [Sequence of Plot or Splot] plots you want to add
|
145
|
+
# @return [Multiplot] self
|
146
|
+
# @example
|
102
147
|
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
|
103
148
|
# enlarged_mp = mp.add_plots(Plot.new('exp(x)')).layout([3,1])
|
149
|
+
# # mp IS NOT affected
|
104
150
|
def add_plots(*plots)
|
105
151
|
plots.unshift(0) unless plots[0].is_a?(Numeric)
|
106
152
|
self.class.new(@plots.insert(*plots), @options)
|
@@ -111,14 +157,33 @@ module GnuplotRB
|
|
111
157
|
alias_method :add, :add_plots
|
112
158
|
|
113
159
|
##
|
114
|
-
#
|
160
|
+
# Destructive version of #add_plots.
|
161
|
+
#
|
162
|
+
# @return [Multiplot] self
|
163
|
+
# @example
|
164
|
+
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
|
165
|
+
# mp.add_plots!(Plot.new('exp(x)')).layout([3,1])
|
166
|
+
# # mp IS affected
|
167
|
+
def add_plots!(*plots)
|
168
|
+
plots.unshift(0) unless plots[0].is_a?(Numeric)
|
169
|
+
@plots = @plots.insert(*plots)
|
170
|
+
self
|
171
|
+
end
|
172
|
+
|
173
|
+
alias_method :add_plot!, :add_plots!
|
174
|
+
alias_method :add!, :add_plots!
|
175
|
+
|
176
|
+
##
|
115
177
|
# Create new Multiplot without plot at given position
|
116
178
|
# (by default last plot is removed).
|
117
|
-
#
|
118
|
-
#
|
119
|
-
#
|
179
|
+
#
|
180
|
+
# @param position [Integer] position of plot which you need to remove
|
181
|
+
# (by default last plot is removed)
|
182
|
+
# @return [Multiplot] self
|
183
|
+
# @example
|
120
184
|
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
|
121
185
|
# mp_with_only_cos = mp.remove_plot(0)
|
186
|
+
# # mp IS NOT affected
|
122
187
|
def remove_plot(position = -1)
|
123
188
|
self.class.new(@plots.delete_at(position), @options)
|
124
189
|
end
|
@@ -126,7 +191,21 @@ module GnuplotRB
|
|
126
191
|
alias_method :remove, :remove_plot
|
127
192
|
|
128
193
|
##
|
129
|
-
#
|
194
|
+
# Destructive version of #remove_plot.
|
195
|
+
#
|
196
|
+
# @return [Multiplot] self
|
197
|
+
# @example
|
198
|
+
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
|
199
|
+
# mp.remove_plot!(0)
|
200
|
+
# # mp IS affected
|
201
|
+
def remove_plot!(position = -1)
|
202
|
+
@plots = @plots.delete_at(position)
|
203
|
+
self
|
204
|
+
end
|
205
|
+
|
206
|
+
alias_method :remove!, :remove_plot!
|
207
|
+
|
208
|
+
##
|
130
209
|
# Equal to #plots[*args]
|
131
210
|
def [](*args)
|
132
211
|
@plots[*args]
|