1
1
module GnuplotRB
2
2
##
3
- # === Overview
4
3
# Multiplot allows to place several plots on one layout.
5
4
# It's usage is covered in
6
5
# {multiplot notebook}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/multiplot_layout.ipynb].
7
6
#
8
- # === Options
9
- # Most of Multiplot options are the same as in Plot.
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
+ #
10
12
# There are only 2 specific options:
11
13
# * title - set title for the whole layout (above all the plots)
12
14
# * layout - set layout size, examples:
13
- # { layout : [1, 3] } - 3 plots, 1 row, 3 columns
14
- # { layout : [2, 2] } - 4 plots, 2 rows, 2 columns
15
+ # { layout : [1, 3] } # 3 plots, 1 row, 3 columns
16
+ # { layout : [2, 2] } # 4 plots, 2 rows, 2 columns
15
17
class Multiplot
16
18
include Plottable
17
19
##
18
- # Array of plots contained by this object.
20
+ # @return [ Array] Array of plots contained by this object
19
21
attr_reader :plots
20
22
21
23
##
22
- # ====== Arguments
23
- # * *plots* are Plot or Splot objects which should be placed
24
- # on this multiplot layout
25
- # * *options* will be considered as 'settable' options of gnuplot
26
- # ('set xrange [1:10]' for { xrange: 1..10 } etc) just as in Plot.
27
- # 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
28
27
def initialize ( *plots , **options )
29
28
@plots = plots [ 0 ] . is_a? ( Hamster ::Vector ) ? plots [ 0 ] : Hamster ::Vector . new ( plots )
30
29
@options = Hamster . hash ( options )
31
30
OptionHandling . validate_terminal_options ( @options )
32
31
end
33
32
34
33
##
35
- # ====== Overview
36
- # This outputs all the plots to term (if given) or to this
37
- # Multiplot's own terminal.
38
- # ====== Arguments
39
- # * *term* - Terminal to plot to
40
- # * *options* - will be considered as 'settable' options of gnuplot
41
- # ('set xrange [1:10]', 'set title 'plot'' etc)
42
- # Options passed here have priority over already existing.
43
- # Inner options of Plots have the highest priority (except
44
- # :term and :output which are ignored in this case).
34
+ # Output all the plots to term (if given) or to this Multiplot's own terminal.
35
+ #
36
+ # @param term [Terminal] Terminal to plot to
37
+ # @param multiplot_part [Boolean] placeholder, does not really needed and should not be used
38
+ # @param options [Hash] see options in top class docs.
39
+ # Options passed here have priority over already set.
40
+ # @return [Multiplot] self
45
41
def plot ( term = nil , multiplot_part : false , **options )
46
42
plot_options = mix_options ( options ) do |plot_opts , mp_opts |
47
43
plot_opts . merge ( multiplot : mp_opts . to_h )
@@ -59,21 +55,25 @@ def plot(term = nil, multiplot_part: false, **options)
59
55
end
60
56
61
57
##
62
- # ====== Overview
63
- # Create new Multiplot object where plot (Plot or Splot object)
64
- # at *position* will
58
+ # Create new updated Multiplot object
59
+ # where plot (Plot or Splot object) at *position* will
65
60
# be replaced with the new one created from it by updating.
66
61
# To update a plot you can pass some options for it or a
67
62
# block, that should take existing plot (with new options if
68
63
# you gave them) and return a plot too.
69
- # ====== Arguments
70
- # * *position* - position of plot which you need to update
64
+ #
65
+ # Method yields new created Plot or Splot to allow you update it manually.
66
+ #
67
+ # @param position [Integer] position of plot which you need to update
71
68
# (by default first plot is updated)
72
- # * *options* - options to update plot with
73
- # * *&block* - method also may take a block which returns a plot
74
- # ====== Example
69
+ # @param options [Hash] options to set into updated plot
70
+ # @return [Multiplot] - self
71
+ # @yieldparam plot [Plot, Splot] a new plot
72
+ # @yieldreturn [Plot, Splot] changed plot
73
+ # @example
75
74
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
76
- # updated_mp = mp.update_plot(title: 'Sin(x) and Exp(x)') { |sinx| sinx.add('exp(x)') }
75
+ # updated_mp = mp.update_plot(title: 'Sin(x) and Exp(x)') { |sinx| sinx.add!('exp(x)') }
76
+ # # mp IS NOT affected
77
77
def update_plot ( position = 0 , **options )
78
78
return self unless block_given? if options . empty?
79
79
replacement = @plots [ position ] . options ( options )
@@ -83,6 +83,14 @@ def update_plot(position = 0, **options)
83
83
84
84
alias_method :update , :update_plot
85
85
86
+ ##
87
+ # Destructive version of #update_plot.
88
+ #
89
+ # @return [Multiplot] - self
90
+ # @example
91
+ # Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
92
+ # mp.update_plot!(title: 'Sin(x) and Exp(x)') { |sinx| sinx.add!('exp(x)') }
93
+ # # mp IS affected
86
94
def update_plot! ( position = 0 , **options )
87
95
return self unless block_given? if options . empty?
88
96
replacement = @plots [ position ] . options! ( options )
@@ -93,22 +101,31 @@ def update_plot!(position = 0, **options)
93
101
alias_method :update! , :update_plot!
94
102
95
103
##
96
- # ====== Overview
97
104
# Create new Multiplot object where plot (Plot or Splot object)
98
105
# at *position* will be replaced with the given one.
99
- # ====== Arguments
100
- # * *position* - position of plot which you need to update
101
- # (by default first plot is updated)
102
- # * *plot* - replacement for existing plot
103
- # ====== Example
106
+ #
107
+ # @param position [Integer] position of plot which you need to replace
108
+ # (by default first plot is replace)
109
+ # @param plot [Plot, Splot] replacement
110
+ # @return [Multiplot] - self
111
+ # @example
104
112
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
105
113
# mp_with_replaced_plot = mp.replace_plot(Plot.new('exp(x)', title: 'exp instead of sin'))
114
+ # # mp IS NOT affected
106
115
def replace_plot ( position = 0 , plot )
107
116
self . class . new ( @plots . set ( position , plot ) , @options )
108
117
end
109
118
110
119
alias_method :replace , :replace_plot
111
120
121
+ ##
122
+ # Destructive version of #replace_plot.
123
+ #
124
+ # @return [Multiplot] - self
125
+ # @example
126
+ # mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
127
+ # mp.replace_plot!(Plot.new('exp(x)', title: 'exp instead of sin'))
128
+ # # mp IS affected
112
129
def replace_plot! ( position = 0 , plot )
113
130
@plots = @plots . set ( position , plot )
114
131
self
@@ -118,15 +135,17 @@ def replace_plot!(position = 0, plot)
118
135
alias_method :[]= , :replace_plot!
119
136
120
137
##
121
- # ====== Overview
122
138
# Create new Multiplot with given *plots* added before plot at given *position*.
123
139
# (by default it adds plot at the front).
124
- # ====== Arguments
125
- # * *position* - position before which you want to add a plot
126
- # * *plots* - sequence of plots you want to add
127
- # ====== Example
140
+ #
141
+ # @param position [Integer] position of plot which you need to replace
142
+ # (by default first plot is replace)
143
+ # @param plots [Sequence of Plot or Splot] plots you want to add
144
+ # @return [Multiplot] - self
145
+ # @example
128
146
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
129
147
# enlarged_mp = mp.add_plots(Plot.new('exp(x)')).layout([3,1])
148
+ # # mp IS NOT affected
130
149
def add_plots ( *plots )
131
150
plots . unshift ( 0 ) unless plots [ 0 ] . is_a? ( Numeric )
132
151
self . class . new ( @plots . insert ( *plots ) , @options )
@@ -136,6 +155,14 @@ def add_plots(*plots)
136
155
alias_method :<< , :add_plots
137
156
alias_method :add , :add_plots
138
157
158
+ ##
159
+ # Destructive version of #add_plots.
160
+ #
161
+ # @return [Multiplot] - self
162
+ # @example
163
+ # mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
164
+ # mp.add_plots!(Plot.new('exp(x)')).layout([3,1])
165
+ # # mp IS affected
139
166
def add_plots! ( *plots )
140
167
plots . unshift ( 0 ) unless plots [ 0 ] . is_a? ( Numeric )
141
168
@plots = @plots . insert ( *plots )
@@ -146,20 +173,30 @@ def add_plots!(*plots)
146
173
alias_method :add! , :add_plots!
147
174
148
175
##
149
- # ====== Overview
150
176
# Create new Multiplot without plot at given position
151
177
# (by default last plot is removed).
152
- # ====== Arguments
153
- # * *position* - position of plot you want to remove
154
- # ====== Example
178
+ #
179
+ # @param position [Integer] position of plot which you need to remove
180
+ # (by default last plot is removed)
181
+ # @return [Multiplot] - self
182
+ # @example
155
183
# mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
156
184
# mp_with_only_cos = mp.remove_plot(0)
185
+ # # mp IS NOT affected
157
186
def remove_plot ( position = -1 )
158
187
self . class . new ( @plots . delete_at ( position ) , @options )
159
188
end
160
189
161
190
alias_method :remove , :remove_plot
162
191
192
+ ##
193
+ # Destructive version of #remove_plot.
194
+ #
195
+ # @return [Multiplot] - self
196
+ # @example
197
+ # mp = Multiplot.new(Plot.new('sin(x)'), Plot.new('cos(x)'), layout: [2,1])
198
+ # mp.remove_plot!(0)
199
+ # # mp IS affected
163
200
def remove_plot! ( position = -1 )
164
201
@plots = @plots . delete_at ( position )
165
202
self
@@ -168,7 +205,6 @@ def remove_plot!(position = -1)
168
205
alias_method :remove! , :remove_plot!
169
206
170
207
##
171
- # ====== Overview
172
208
# Equal to #plots[*args]
173
209
def []( *args )
174
210
@plots [ *args ]
0 commit comments