{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Updating data in plots\n",
"#### Data used in the following examples"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x = (1..600).map { |i| i/100.0 }\n",
"y = x.map { |xx| Math.sin(xx ** 2.3) * Math.exp(-xx) }\n",
"nil"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Updating data stored in file\n",
"First lets generate a file"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1..300"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"File.open('tons_of_data', 'w') { |f| (1..300).each { |i| f.puts \"#{x[i]} #{y[i]}\" } }"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and plot it"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"# \"Tons of data\", :with => \"lines\"]>], @options=Hamster::Hash[], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"require 'gnuplotrb'\n",
"include GnuplotRB\n",
"\n",
"graph = Plot.new(['tons_of_data', title: 'Tons of data', with: 'lines'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can just append file and replot out graph"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"# \"Tons of data\", :with => \"lines\"]>], @options=Hamster::Hash[], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"File.open('tons_of_data', 'a') { |f| (301..600).each { |i| f.puts \"#{x[i]} #{y[i]}\" } }\n",
"\n",
"graph"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Updating data given as points array\n",
"##### 1. Data stored in memory and piped to gnuplot in here-doc"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:title => \"Before update\", :with => \"lines\"]>], @options=Hamster::Hash[], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x0 = x[0..200]\n",
"y0 = y[0..200]\n",
"updatable_plot = Plot.new([[x0,y0], with: 'lines', title: 'Before update'])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:title => \"After update\", :with => \"lines\"]>], @options=Hamster::Hash[], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1 = x[201..600]\n",
"y1 = y[201..600]\n",
"updatable_plot.update_dataset(data: [x1,y1], title: 'After update')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:title => \"Before update\", :with => \"lines\"]>], @options=Hamster::Hash[], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"updatable_plot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### 2. Data stored in temporary file and its name is piped to gnuplot\n",
"Dataset boolean option 'file' corresponds to using temporary file for storage data being plotted."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:title => \"Before update\", :with => \"lines\"]>], @options=Hamster::Hash[], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x0 = x[0..400]\n",
"y0 = y[0..400]\n",
"updatable_file_plot = Plot.new([[x0,y0], with: 'lines', title: 'Before update', file: true])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:title => \"After update\", :with => \"lines\"]>], @options=Hamster::Hash[], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1 = x[401..600]\n",
"y1 = y[401..600]\n",
"updatable_file_plot.update_dataset(data: [x1,y1], title: 'After update')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Be careful**: updating data stored in temp file **affects** original Plot:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:title => \"Before update\", :with => \"lines\"]>], @options=Hamster::Hash[], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"updatable_file_plot"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 2.1.2",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": "rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "2.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}