{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Scatterplot with statsample\n", "\n", "Creating a scatterplot with statsample's Statsample::Graph::Scatterplot class.\n", " \n", "In this example we'll demonstrate how a normally distributed Daru::Vector can be created using the daru and distribution gems, and how the values generated can be plotted very easily using the 'scatterplot' shorthand and supplying X and Y co-ordinates.\n", "\n", "Statsample uses rubyvis internally for generating plots." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "if(window['d3'] === undefined ||\n", " window['Nyaplot'] === undefined){\n", " var path = {\"d3\":\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\",\"downloadable\":\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\"};\n", "\n", "\n", "\n", " var shim = {\"d3\":{\"exports\":\"d3\"},\"downloadable\":{\"exports\":\"downloadable\"}};\n", "\n", " require.config({paths: path, shim:shim});\n", "\n", "\n", "require(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\n", "\n", "\tvar script = d3.select(\"head\")\n", "\t .append(\"script\")\n", "\t .attr(\"src\", \"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\")\n", "\t .attr(\"async\", true);\n", "\n", "\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\n", "\n", "\n", "\t var event = document.createEvent(\"HTMLEvents\");\n", "\t event.initEvent(\"load_nyaplot\",false,false);\n", "\t window.dispatchEvent(event);\n", "\t console.log('Finished loading Nyaplotjs');\n", "\n", "\t};\n", "\n", "\n", "});});\n", "}\n" ], "text/plain": [ "\"if(window['d3'] === undefined ||\\n window['Nyaplot'] === undefined){\\n var path = {\\\"d3\\\":\\\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min\\\",\\\"downloadable\\\":\\\"https://cdn.rawgit.com/domitry/d3-downloadable/master/d3-downloadable\\\"};\\n\\n\\n\\n var shim = {\\\"d3\\\":{\\\"exports\\\":\\\"d3\\\"},\\\"downloadable\\\":{\\\"exports\\\":\\\"downloadable\\\"}};\\n\\n require.config({paths: path, shim:shim});\\n\\n\\nrequire(['d3'], function(d3){window['d3']=d3;console.log('finished loading d3');require(['downloadable'], function(downloadable){window['downloadable']=downloadable;console.log('finished loading downloadable');\\n\\n\\tvar script = d3.select(\\\"head\\\")\\n\\t .append(\\\"script\\\")\\n\\t .attr(\\\"src\\\", \\\"https://cdn.rawgit.com/domitry/Nyaplotjs/master/release/nyaplot.js\\\")\\n\\t .attr(\\\"async\\\", true);\\n\\n\\tscript[0][0].onload = script[0][0].onreadystatechange = function(){\\n\\n\\n\\t var event = document.createEvent(\\\"HTMLEvents\\\");\\n\\t event.initEvent(\\\"load_nyaplot\\\",false,false);\\n\\t window.dispatchEvent(event);\\n\\t console.log('Finished loading Nyaplotjs');\\n\\n\\t};\\n\\n\\n});});\\n}\\n\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " −2.0\n", " −1.5\n", " −1.0\n", " −0.5\n", " 0.5\n", " 1.0\n", " 1.5\n", " 2.0\n", " 2.5\n", " 3.0\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " 0.5\n", " 1.0\n", " 1.5\n", " 2.0\n", " 2.5\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "" ], "text/plain": [ "#\n", " nil\n", " 0 0.8555717732852323\n", " 1 1.3796604049245773\n", " 2 -1.4593861192824076\n", " 3 -0.4699957520721252\n", " 4 1.4090689364977902\n", " 5 1.6183257065797851\n", " 6 -0.07780768945500717\n", " 7 0.6499839472222753\n", " 8 -1.3419021519707346\n", " 9 1.2485312487270923\n", " 10 0.1941360457299193\n", " 11 -0.37493440901722397\n", " 12 -1.0348745316277477\n", " 13 0.5497337779257621\n", " 14 -2.1720385693414097\n", " 15 -0.10135197134293111\n", " 16 0.851311173395707\n", " ... ...\n", ", @v2=\n", "#\n", " nil\n", " 0 1.842901828988894\n", " 1 1.5121559391371144\n", " 2 -0.7666998640965631\n", " 3 -0.1255772078119607\n", " 4 1.7163629531044808\n", " 5 1.9400292540789408\n", " 6 0.5137542918625461\n", " 7 1.2183809110632908\n", " 8 -0.6866998695840855\n", " 9 1.8088367619975951\n", " 10 0.9362356439099947\n", " 11 0.15448151852048897\n", " 12 -0.810652668548135\n", " 13 0.9188575625630161\n", " 14 -1.7519699609042996\n", " 15 0.5586538555949065\n", " 16 1.8080815355409132\n", " ... ...\n", ", @opts={:name=>\"Scatterplot ( - )\", :width=>400, :height=>300, :dot_alpha=>0.5, :line_median=>false, :margin_top=>10, :margin_bottom=>20, :margin_left=>20, :margin_right=>20, :minimum_x=>nil, :maximum_x=>nil, :minimum_y=>nil, :maximum_y=>nil, :groups=>nil}, @name=\"Scatterplot ( - )\", @width=400, @height=300, @dot_alpha=0.5, @line_median=false, @margin_top=10, @margin_bottom=20, @margin_left=20, @margin_right=20, @minimum_x=-2.932986206842199, @maximum_x=2.5570151538922468, @minimum_y=-2.3985656481466044, @maximum_y=3.274272065233321, @groups=nil, @data=[{:x=>0.8555717732852323, :y=>1.842901828988894}, {:x=>1.3796604049245773, :y=>1.5121559391371144}, {:x=>-1.4593861192824076, :y=>-0.7666998640965631}, {:x=>-0.4699957520721252, :y=>-0.1255772078119607}, {:x=>1.4090689364977902, :y=>1.7163629531044808}, {:x=>1.6183257065797851, :y=>1.9400292540789408}, {:x=>-0.07780768945500717, :y=>0.5137542918625461}, {:x=>0.6499839472222753, :y=>1.2183809110632908}, {:x=>-1.3419021519707346, :y=>-0.6866998695840855}, {:x=>1.2485312487270923, :y=>1.8088367619975951}, {:x=>0.1941360457299193, :y=>0.9362356439099947}, {:x=>-0.37493440901722397, :y=>0.15448151852048897}, {:x=>-1.0348745316277477, :y=>-0.810652668548135}, {:x=>0.5497337779257621, :y=>0.9188575625630161}, {:x=>-2.1720385693414097, :y=>-1.7519699609042996}, {:x=>-0.10135197134293111, :y=>0.5586538555949065}, {:x=>0.851311173395707, :y=>1.8080815355409132}, {:x=>-0.42006370775565166, :y=>0.21452105132958338}, {:x=>1.3576775028909045, :y=>1.9451100590205945}, {:x=>-1.1769029682031389, :y=>-0.593598225803356}, {:x=>1.0497361648298074, :y=>1.7088683567998102}, {:x=>-0.42674611284108344, :y=>0.24600110759892702}, {:x=>-0.00848904024833469, :y=>0.39614436704386324}, {:x=>0.02563747410353377, :y=>0.5792951153591217}, {:x=>0.6389600321610811, :y=>1.1068941185344268}, {:x=>-0.02680568897714934, :y=>0.7635162006063356}, {:x=>0.5994248667769698, :y=>1.283381307691144}, {:x=>-0.29395591318613223, :y=>0.2812504010669228}, {:x=>-0.6641920685349407, :y=>0.2816811650010417}, {:x=>0.9165469082690338, :y=>1.258887567256162}, {:x=>-1.306087534362218, :y=>-0.4937337153580432}, {:x=>0.9844887735181108, :y=>1.2380099188892038}, {:x=>0.6299284635108594, :y=>1.1921909895053413}, {:x=>0.2005780886149533, :y=>0.6598972703633497}, {:x=>-1.1939489809695338, :y=>-0.889712273696468}, {:x=>-2.235487424364157, :y=>-1.4676941869754234}, {:x=>0.3699846686059001, :y=>1.0373457279792433}, {:x=>-0.28420757184721723, :y=>0.05917417697587557}, {:x=>1.1555301876605601, :y=>1.5021235409448148}, {:x=>0.3311251532534688, :y=>0.8703101765028506}, {:x=>-0.9817653362902174, :y=>-0.45975035040217604}, {:x=>0.34643958721312923, :y=>0.723054422118439}, {:x=>0.9732732527284514, :y=>1.7514785279301313}, {:x=>-0.14405430694692123, :y=>0.4005508167859013}, {:x=>-1.234164370973656, :y=>-0.6785347525205356}, {:x=>1.0436445116349982, :y=>1.3753244946108212}, {:x=>-0.6142128118026866, :y=>-0.2950315380292299}, {:x=>-0.48299069080902285, :y=>-0.3782429405249634}, {:x=>-2.265602857662952, :y=>-1.9296495932967814}, {:x=>-0.5615124991022293, :y=>0.21429175985473026}, {:x=>-1.977520033626085, :y=>-1.4744133044943992}, {:x=>-1.620223341940157, :y=>-1.2556512493560403}, {:x=>-0.011056586123090824, :y=>0.48016585453591437}, {:x=>0.25164936144970773, :y=>0.7779638322600364}, {:x=>0.7693824409162502, :y=>1.0439830200203142}, {:x=>0.11226773894055957, :y=>0.5068915752404018}, {:x=>-0.22065433630416426, :y=>-0.06515235488139692}, {:x=>-0.4532976194845781, :y=>0.11664639001127369}, {:x=>0.7509165945504802, :y=>1.1840293826838002}, {:x=>0.0744150936608453, :y=>0.7848465957060311}, {:x=>0.5722859601994426, :y=>0.8356610717437039}, {:x=>0.5086036408026927, :y=>1.2595745720544278}, {:x=>1.3498051753763403, :y=>1.9805879554638013}, {:x=>2.094781324988435, :y=>2.4650125966560945}, {:x=>2.2303923940042973, :y=>2.3315417732547}, {:x=>1.081328138136123, :y=>1.674255450612876}, {:x=>0.09328541458824126, :y=>0.6357869049416018}, {:x=>2.0208246552111278, :y=>2.491491464189869}, {:x=>-0.3244848617569078, :y=>0.035322672889444684}, {:x=>-0.37202614425603736, :y=>0.18767829901441807}, {:x=>0.3687499051376125, :y=>1.0148530040901946}, {:x=>0.9058577478162074, :y=>1.6218101796634743}, {:x=>-0.16614402157004216, :y=>0.662237338086057}, {:x=>0.6510869571519802, :y=>0.9711612363823994}, {:x=>1.5992078080042134, :y=>2.4552001949324254}, {:x=>0.6342064299553272, :y=>1.2130274259520282}, {:x=>0.05003385495259036, :y=>0.5387947529011584}, {:x=>0.9598291156010675, :y=>1.5574650999254926}, {:x=>-0.88709050082111, :y=>-0.404059626853843}, {:x=>1.7120181207587648, :y=>2.0984202859482908}, {:x=>-0.9805667778538752, :y=>-0.04164832336787405}, {:x=>1.1098025103698166, :y=>1.6038408439062908}, {:x=>1.8781991420133932, :y=>2.2544149949254435}, {:x=>0.40370262958812075, :y=>0.760074347409746}, {:x=>0.07866587999565865, :y=>0.0471093907196095}, {:x=>-0.7802118720205637, :y=>-0.35218167482515184}, {:x=>-0.7067310059325819, :y=>-0.0436667696229347}, {:x=>-1.1342241277168794, :y=>-1.0920027653725444}, {:x=>0.9018743619718148, :y=>1.5218814661070543}, {:x=>-0.8404946502670552, :y=>-0.22368446980723677}, {:x=>-2.932986206842199, :y=>-2.3985656481466044}, {:x=>0.5376196431622354, :y=>1.1540585182079353}, {:x=>1.8058035125001475, :y=>2.091200639153208}, {:x=>-0.7225243640129502, :y=>-0.02654446043419889}, {:x=>0.3754788847179366, :y=>0.7389161893428515}, {:x=>-0.7619378746730044, :y=>-0.586068052327788}, {:x=>0.534622070957472, :y=>1.0250372350505366}, {:x=>2.5570151538922468, :y=>3.274272065233321}, {:x=>-0.2560085134878167, :y=>0.3501241157591689}, {:x=>0.799629851381487, :y=>1.644435251056487}], @x_scale=#], @type=:number, @n=true, @f=#, @g=#, @tick_format=#>, @y_scale=#], @type=:number, @n=true, @f=#, @g=#, @tick_format=#>>" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "require 'statsample'\n", "include Statsample::Shorthand\n", "\n", "n = 100\n", "x = rnorm(n)\n", "y = x + rnorm(n,0.5,0.2)\n", "Statsample::Graph::Scatterplot.new(x,y)" ] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.2.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.2.1" } }, "nbformat": 4, "nbformat_minor": 0 }