Class: BPlot
- Inherits:
-
Object
- Object
- BPlot
- Defined in:
- lib/bplot.rb
Overview
BPlot is a 2D and 3D plotting module for SciRuby. It provides a simple API that will be familiar to Gnuplot users and to a lesser extent Matlab users. BPlot is in ALPHA status. Two dimmensional plots seem to work well but they really need more testing. Also, the API is not yet settled and may change without warning. Many features are not yet implemented, including surface plots.
Instance Method Summary collapse
-
#cmd(str) ⇒ Object
Issue raw command.
-
#initialize ⇒ BPlot
constructor
Create new engine.
-
#multiplot(opts = '') ⇒ Object
Multiplot.
-
#plot(*args) ⇒ Object
2D Plotting.
-
#refresh ⇒ Object
Refresh.
-
#set(str) ⇒ Object
Settings.
-
#show(str) ⇒ Object
Show.
-
#unset(str) ⇒ Object
Settings.
Constructor Details
#initialize ⇒ BPlot
Create new engine
Every call to BPlot#new creates a new instance of Gnuplot. You can have multiple instances of Gnuplot in the same Ruby script.
Example
b.BPlot.new
b.cmd('plot sin(x)')
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bplot.rb', line 52 def initialize() # # Every instance of BPlot has its own gnuplot process. # @pipe = IO.popen("gnuplot -p","w") # # Configure Gnuplot. # @pipe.puts "set terminal wxt" # Nice GUI for plots. @pipe.puts "set termoption dashed" # Needed for 'r--', 'r.-', 'r:' @pipe.puts "set termoption enhanced" # Support superscripts + subscripts. end |
Instance Method Details
#cmd(str) ⇒ Object
Issue raw command
Send a raw command to the Gnuplot backend. This gives the user more control over plotting engine.
Example
b.BPlot.new
b.cmd('plot sin(x)')
77 78 79 |
# File 'lib/bplot.rb', line 77 def cmd(str) @pipe.puts str end |
#multiplot(opts = '') ⇒ Object
Multiplot
In multiplot mode, multiple plot commands are placed togeher in the same window. This can be a convenient alternative to having a very long multiple-line plot command. It allso allows you to add plots using loops and other constructions.
Examples
b.multiplot do
plot(x, y1)
plot(x, y2)
plot(x, y3)
end
b.multiplot do
(0..10).each { |t| plot( x, sin(x + v*t) ) }
end
163 164 165 166 167 |
# File 'lib/bplot.rb', line 163 def multiplot(opts='') @pipe.puts "set multiplot #{opts}" yield if block_given? @pipe.puts "unset multiplot" end |
#plot(*args) ⇒ Object
2D Plotting
This is the key method in BPlot, as it is responsible for all 2D plots. This method is currently in a state of flux and is for the moment undocumented.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/bplot.rb', line 173 def plot(*args) all_data = [] all_plots = '' while args.length > 0 # # NEXT PLOT # # Anything that is of class Array or NMatrix is data. this_data = [] while args[0].class == 'Array' or args[0].class == 'NMatrix' this_data << args.shift end all_data << this_data # - Get the settings for this plot. # - If 'args' is not empty, there is another plot. this_plot, args = styles(args) all_plots << this_plot all_plots << ', ' if args.length > 0 end # TODO: # - Check for both Array and NMatrix. # - But make sure the program works without NMatrix loaded. # # # Each plot needs a separate stream of data separated by 'e' ("end"). # nblocks = all_data.length stream = (1..nblocks).map { |s| ncols = all_data[s].length nrows = all_data[s][0].class == 'Array' ? all_data[s][0].length : all_data[s][0].shape[0] - 1 (0..nrows).map { |r| (0..ncols).map { |c| " " + all_data[s][c][r] }.join + "\n" }.join + "e\n" }.join # stream = (1..nstreams).map { |s| # x = data.shift # y = data.shift # n = x.is_a?(Array) ? x.length - 1 : x.shape[0] - 1 # # (0..n).map { |i| "#{x[i]} #{y[i]}\n" }.join + "e\n" # }.join @pipe.puts "plot #{all_plots} \n#{stream}" end |
#refresh ⇒ Object
Refresh
This command reformats and redraws the current plot with the latest settings. This is useful for viewing a plot with different “set” options, or for generating the same plot for several output formats.
Example
b.set('terminal postscript')
b.set('output "myplot.ps"')
b.refresh
b.set('terminal png')
b.set('output "myplot.png"')
b.refresh
141 142 143 |
# File 'lib/bplot.rb', line 141 def refresh() @pipe.puts "refresh" end |
#set(str) ⇒ Object
Settings
This method is a thin wrapper around the Gnuplot “set” command. It can be used to set *a lot* of options. However, nothing is drawn until the user issues a plotting command. See the examples below for a quick overview. See the Gnuplot documentation for more details.
Examples
b.set('terminal enhanced postscript color')
b.set('output "myplot.ps"')
b.set('xrange [0:10]')
b.set('yrange [0:50]')
b.set('xlabel "This is the X Axis"')
b.set('ylabel "This is the Y Axis"')
b.set('title "A title for the whole plot"')
100 101 102 |
# File 'lib/bplot.rb', line 100 def set(str) @pipe.puts "set #{str}" end |
#show(str) ⇒ Object
Show
The set command can be used to set lots of options. No screen is drawn, however, until a plotting command is given. The show command shows their settings; show(‘all’) shows all the settings.
120 121 122 |
# File 'lib/bplot.rb', line 120 def show(str) @pipe.puts "show #{str}" end |
#unset(str) ⇒ Object
Settings
This method is a thin wrapper around the Gnuplot “unset” command. It is used to reset settings back to their default values. See the documentation on “set” for more information. See also the Gnuplot documentaion for “set” and “unset”.
110 111 112 |
# File 'lib/bplot.rb', line 110 def unset(str) @pipe.puts "unset #{str}" end |