1
1
module GnuplotRB
2
2
##
3
- # === Overview
4
3
# Class corresponding to simple 2D visualisation.
5
4
#
6
- # === Notebooks
5
+ # == Notebooks
7
6
#
8
7
# * {Heatmaps}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/heatmaps.ipynb]
9
8
# * {Vector field}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/vector_field.ipynb]
10
9
# * {Math equations}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/math_plots.ipynb]
11
10
# * {Histogram}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/histogram.ipynb]
12
11
# * {Updating plots with new data}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/updating_data.ipynb]
13
12
#
14
- # === Options
13
+ # == Options
15
14
# All possible options are exaplained in
16
- # {gnuplot docs}[http://www.gnuplot.info/docs_5.0/gnuplot.pdf](pp. 105-190).
15
+ # {gnuplot docs}[http://www.gnuplot.info/docs_5.0/gnuplot.pdf] (pp. 105-190).
16
+ #
17
17
# Several common ones:
18
+ #
18
19
# * xrange(yrange, zrange, urange, vrange) - set range for a variable. Takes
19
20
# Range (xrange: 0..100), or String (yrange: '[0:100]').
20
21
# * title - plot's title. Takes String (title: 'Some new plot').
@@ -28,21 +29,22 @@ module GnuplotRB
28
29
# due to existance of #to_<term_name> methods. One should use #to_png('file.png') instead of
29
30
# passing { term: 'png', output: 'file.png' }.
30
31
# Every option may be passed to constructor in order to create plot with it.
32
+ #
31
33
# Methods #options(several: options, ...) and bunch of #option_name(only_an: option) such as
32
34
# #xrange, #using, #polar etc create new Plot object based on existing but with a new options.
33
- # See notebooks for examples.
35
+ #
36
+ # Methods with the same names ending with '!' or '=' ('plot.xrange!(1..3)',
37
+ # 'plot.title = "New title"') are destructive and modify state of existing object just as
38
+ # "Array#sort!" do with Array object. See notebooks for examples.
34
39
class Plot
35
40
include Plottable
36
41
##
37
42
# Array of datasets which are plotted by this object.
38
43
attr_reader :datasets
39
44
##
40
- # ====== Arguments
41
- # * *datasets* are either instances of Dataset class or
42
- # [data, **dataset_options] arrays from which Dataset may be created
43
- # * *options* will be considered as 'settable' options of gnuplot
44
- # ('set xrange [1:10]' for { xrange: 1..10 },
45
- # "set title 'plot'" for { title: 'plot' } etc).
45
+ # @param *datasets [Sequence of Dataset or Array] either instances of Dataset class or
46
+ # "[data, **dataset_options]"" arrays
47
+ # @param options [Hash] see Plot top level doc for options examples
46
48
def initialize ( *datasets )
47
49
# had to relace **options arg with this because in some cases
48
50
# Daru::DataFrame was mentioned as hash and added to options
@@ -59,14 +61,13 @@ def initialize(*datasets)
59
61
end
60
62
61
63
##
62
- # ====== Overview
63
- # This outputs plot to term (if given) or to this plot's own terminal.
64
- # ====== Arguments
65
- # * *term* - Terminal object to plot to
66
- # * *multiplot_part* - part of a multiplot. Option for inner usage
67
- # * *options* - will be considered as 'settable' options of gnuplot
68
- # ('set xrange [1:10]', 'set title 'plot'' etc)
69
- # Options passed here have priority over already existing.
64
+ # Output plot to term (if given) or to this plot's own terminal.
65
+ #
66
+ # @param term [Terminal] Terminal object to plot to
67
+ # @param :multiplot_part [Boolean] true if this plot is part of a multiplot. For inner use!
68
+ # @param options [Hash] see options in Plot top level doc.
69
+ # Options passed here have priority over already existing.
70
+ # @return [Plot] self
70
71
def plot ( term = nil , multiplot_part : false , **options )
71
72
fail ArgumentError , 'Empty plots are not supported!' if @datasets . empty?
72
73
inner_opts = if multiplot_part
@@ -91,44 +92,68 @@ def plot(term = nil, multiplot_part: false, **options)
91
92
alias_method :replot , :plot
92
93
93
94
##
94
- # ====== Overview
95
95
# Create new Plot object where dataset at *position* will
96
96
# be replaced with the new one created from it by updating.
97
- # ====== Arguments
98
- # * * position* - position of dataset which you need to update
97
+ #
98
+ # @param position [Integer] position of dataset which you need to update
99
99
# (by default first dataset is updated)
100
- # * *data* - data to update dataset with
101
- # * *options* - options to update dataset with
102
- # ====== Example
100
+ # @param data [#to_gnuplot_points] data to update dataset with
101
+ # @param options [Hash] options to update dataset with, see Dataset top level doc
102
+ #
103
+ # @example
103
104
# updated_plot = plot.update_dataset(data: [x1,y1], title: 'After update')
105
+ # # plot IS NOT affected (if dataset did not store data in a file)
104
106
def update_dataset ( position = 0 , data : nil , **options )
105
107
old_ds = @datasets [ position ]
106
108
new_ds = old_ds . update ( data , options )
107
109
new_ds . equal? ( old_ds ) ? self : replace_dataset ( position , new_ds )
108
110
end
109
111
112
+ ##
113
+ # Updates existing Plot object by replacing dataset at *position*
114
+ # with the new one created from it by updating.
115
+ #
116
+ # @param position [Integer] position of dataset which you need to update
117
+ # (by default first dataset is updated)
118
+ # @param data [#to_gnuplot_points] data to update dataset with
119
+ # @param options [Hash] options to update dataset with, see Dataset top level doc
120
+ #
121
+ # @example
122
+ # plot.update_dataset!(data: [x1,y1], title: 'After update')
123
+ # # plot IS affected anyway
110
124
def update_dataset! ( position = 0 , data : nil , **options )
111
125
@datasets [ position ] . update! ( data , options )
112
126
self
113
127
end
114
128
115
129
##
116
- # ====== Overview
117
130
# Create new Plot object where dataset at *position* will
118
131
# be replaced with the given one.
119
- # ====== Arguments
120
- # * * position* - position of dataset which you need to update
132
+ #
133
+ # @param position [Integer] position of dataset which you need to replace
121
134
# (by default first dataset is replaced)
122
- # * *dataset* - dataset to replace the old one. You can also
123
- # give here [data, **dataset_options] array from
124
- # which Dataset may be created.
125
- # ====== Example
135
+ # @param dataset [Dataset, Array] dataset to replace the old one. You can also
136
+ # give here "[data, **dataset_options]"" array from which Dataset may be created.
137
+ # @example
126
138
# sinx = Plot.new('sin(x)')
127
139
# cosx = sinx.replace_dataset(['cos(x)'])
140
+ # # sinx IS NOT affected
128
141
def replace_dataset ( position = 0 , dataset )
129
142
self . class . new ( @datasets . set ( position , dataset_from_any ( dataset ) ) , @options )
130
143
end
131
144
145
+ ##
146
+ # Updates existing Plot object by replacing dataset at *position*
147
+ # with the given one.
148
+ #
149
+ # @param position [Integer] position of dataset which you need to replace
150
+ # (by default first dataset is replaced)
151
+ # @param dataset [Dataset, Array] dataset to replace the old one. You can also
152
+ # give here "[data, **dataset_options]"" array from which Dataset may be created.
153
+ # @example
154
+ # sinx = Plot.new('sin(x)')
155
+ # sinx.replace_dataset!(['cos(x)'])
156
+ # # sinx IS affected
132
157
def replace_dataset! ( position = 0 , dataset )
133
158
@datasets = @datasets . set ( position , dataset_from_any ( dataset ) )
134
159
self
@@ -137,18 +162,19 @@ def replace_dataset!(position = 0, dataset)
137
162
alias_method :[]= , :replace_dataset!
138
163
139
164
##
140
- # ====== Overview
141
165
# Create new Plot object where given datasets will
142
166
# be inserted into dataset list before given position
143
167
# (position = 0 by default).
144
- # ====== Arguments
145
- # * *position* - position where to insert given datasets
146
- # * *datasets* - sequence of datasets to add
147
- # ====== Example
168
+ #
169
+ # @param position [Integer] position of dataset BEFORE which datasets will be placed.
170
+ # 0 by default.
171
+ # @param *datasets [ Sequence of Dataset or Array] datasets to insert
172
+ # @example
148
173
# sinx = Plot.new('sin(x)')
149
174
# sinx_and_cosx_with_expx = sinx.add(['cos(x)'], ['exp(x)'])
150
175
#
151
176
# cosx_and_sinx = sinx << ['cos(x)']
177
+ # # sinx IS NOT affected in both cases
152
178
def add_datasets ( *datasets )
153
179
datasets . map! { |ds | ds . is_a? ( Numeric ) ? ds : dataset_from_any ( ds ) }
154
180
# first element is position where to add datasets
@@ -159,6 +185,17 @@ def add_datasets(*datasets)
159
185
alias_method :add_dataset , :add_datasets
160
186
alias_method :<< , :add_datasets
161
187
188
+ ##
189
+ # Updates existing Plot object by inserting given datasets
190
+ # into dataset list before given position (position = 0 by default).
191
+ #
192
+ # @param position [Integer] position of dataset BEFORE which datasets will be placed.
193
+ # 0 by default.
194
+ # @param *datasets [ Sequence of Dataset or Array] datasets to insert
195
+ # @example
196
+ # sinx = Plot.new('sin(x)')
197
+ # sinx.add!(['cos(x)'], ['exp(x)'])
198
+ # # sinx IS affected
162
199
def add_datasets! ( *datasets )
163
200
datasets . map! { |ds | ds . is_a? ( Numeric ) ? ds : dataset_from_any ( ds ) }
164
201
# first element is position where to add datasets
@@ -170,28 +207,37 @@ def add_datasets!(*datasets)
170
207
alias_method :add_dataset! , :add_datasets!
171
208
172
209
##
173
- # ====== Overview
174
210
# Create new Plot object where dataset at given position
175
211
# will be removed from dataset list.
176
- # ====== Arguments
177
- # * * position* - position of dataset that should be
212
+ #
213
+ # @param position [Integer] position of dataset that should be
178
214
# removed (by default last dataset is removed)
179
- # ====== Example
215
+ # @example
180
216
# sinx_and_cosx = Plot.new('sin(x)', 'cos(x)')
181
217
# sinx = sinx_and_cosx.remove_dataset
182
218
# cosx = sinx_and_cosx.remove_dataset(0)
219
+ # # sinx_and_cosx IS NOT affected in both cases
183
220
def remove_dataset ( position = -1 )
184
221
self . class . new ( @datasets . delete_at ( position ) , @options )
185
222
end
186
223
224
+ ##
225
+ # Updates existing Plot object by removing dataset at given position.
226
+ #
227
+ # @param position [Integer] position of dataset that should be
228
+ # removed (by default last dataset is removed)
229
+ # @example
230
+ # sinx_and_cosx = Plot.new('sin(x)', 'cos(x)')
231
+ # sinx_and_cosx!.remove_dataset
232
+ # sinx_and_cosx!.remove_dataset
233
+ # # sinx_and_cosx IS affected and now is empty
187
234
def remove_dataset! ( position = -1 )
188
235
@datasets = @datasets . delete_at ( position )
189
236
self
190
237
end
191
238
192
239
##
193
- # ====== Overview
194
- # The same as Plot#datasets[args]
240
+ # The same as #datasets[*args]
195
241
def []( *args )
196
242
@datasets [ *args ]
197
243
end
0 commit comments