x = (1..600).map { |i| i/100.0 }
y = x.map { |xx| Math.sin(xx ** 2.3) * Math.exp(-xx) }
nil
First lets generate a file
File.open('tons_of_data', 'w') { |f| (1..300).each { |i| f.puts "#{x[i]} #{y[i]}" } }
1..300
and plot it
require 'gnuplotrb'
include GnuplotRB
graph = Plot.new(['tons_of_data', title: 'Tons of data', with: 'lines'])
Now we can just append file and replot out graph
File.open('tons_of_data', 'a') { |f| (301..600).each { |i| f.puts "#{x[i]} #{y[i]}" } }
graph
x0 = x[0..200]
y0 = y[0..200]
updatable_plot = Plot.new([[x0,y0], with: 'lines', title: 'Before update'])
x1 = x[201..600]
y1 = y[201..600]
updatable_plot.update_dataset(data: [x1,y1], title: 'After update')
This way is expensive because data is piped to gnuplot each time you want to replot it, but updating data does not affect original Plot:
updatable_plot
Dataset boolean option 'file' corresponds to using temporary file for storage data being plotted.
x0 = x[0..400]
y0 = y[0..400]
updatable_file_plot = Plot.new([[x0,y0], with: 'lines', title: 'Before update', file: true])
x1 = x[401..600]
y1 = y[401..600]
updatable_file_plot.update_dataset(data: [x1,y1], title: 'After update')
Be careful: updating data stored in temp file affects original Plot:
updatable_file_plot
Almost every update method in GnuplotRB's classes has its destructive twin ending with '!' or '='.
Examples relate to Plot, but Splot, Multiplot, Animation and so on options may be changed the same way too.
plot = Plot.new('sin(x)', title: 'Old title')
plot.title = 'New title'
# plot is container of datasets, so plot[0] is Dataset object
plot[0].title = 'Dataset title'
plot.xrange!(-1..1)
plot.yrange = 0..1
plot
You can update several options at once using #options!
method or use something like Plot#update_dataset!
.Please see GnuplotRB doc for more methods.
Plot and Splot are containers for Dataset, Multiplot and Animation - for Plots and Splots. So sometimes you may want to modify them. GnuplotRB offers methods such as #add_dataset
, #remove_dataset
, #replace_dataset
, #update_dataset
, their destructive twins (with '!' at the end) and methods like them for other classes.
plot = Plot.new('sin(x)', 'cos(x)', 'exp(x)', xrange: 0..2)
plot.replace_dataset(1, 'x*x') # DOES NOT modify original plot
plot.replace_dataset!(2, 'x*x') # MODIFYES original plot
plot.update_dataset!(0, title: '1-st dataset')
plot.remove_dataset!(2)