{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plotting graph with points given in file\n",
"\n",
"#### First we need to have file with points\n",
"In this example I will just generate it."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1..100000"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"File.open('tons_of_data', 'w') do |f|\n",
" (1..100000).each do |x|\n",
" xx = x/1000.0\n",
" yy = Math.exp(Math.sin(xx))\n",
" f.puts \"#{xx} #{yy}\"\n",
" end\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Plot points\n",
"Ok, we have file with points so lets plot them:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"# \"lines\", :title => \"Tons of data\"]>], @options=Hamster::Hash[], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"require 'gnuplotrb'\n",
"include GnuplotRB\n",
"\n",
"Plot.new(['tons_of_data', title: 'Tons of data', with: 'lines'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plotting graph with points given as array\n",
"This example will show how to plot graph from points in polar coordinate system."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"#, @options=Hamster::Hash[:notitle => true, :with => \"filledcurve\", :lt => {:rgb=>\"#BCECFF\"}]>, #, @options=Hamster::Hash[:with => \"lines\", :title => \"Butterfly\", :lt => {:rgb=>\"#422515\", :lw=>2}]>], @options=Hamster::Hash[:polar => true], @already_plotted=true, @cmd=\"plot \", @terminal=#&1\", @current_datablock=0, @err_array=[], @in=#>>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"include Math\n",
"\n",
"def butterfly_point(angle)\n",
" 8 - \n",
" 1.2 * sin(angle) +\n",
" 2 * sin(3 * angle) +\n",
" 2 * sin(5 * angle) -\n",
" sin(7 * angle) + \n",
" 0.8 * sin(9 * angle) -\n",
" 0.3 * sin(11 * angle) +\n",
" 4.8 * cos(2 * angle) -\n",
" 2 * cos(4 * angle) +\n",
" 0.8 * cos(13.2 * angle)\n",
"end\n",
"\n",
"θ = (0.0..10*PI).step(0.01).to_a\n",
"r = θ.map { |angle| butterfly_point(angle) }\n",
"\n",
"butterfly = Plot.new(\n",
" [[θ,r], notitle: true, with: 'filledcurve', lt: {rgb: '#BCECFF'}],\n",
" [[θ,r], title: 'Butterfly', with: 'lines', lt: {rgb: '#422515', lw: 2}],\n",
" polar: true\n",
")"
]
}
],
"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
}