{ "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": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "image/svg+xml": [ "−1.5−1.0−0.50.51.01.52.02.53.00.51.01.52.0" ], "text/plain": [ "#\n", " nil\n", " 0 1.2029523425685384\n", " 1 -0.7284367836633963\n", " 2 -0.7147373926166352\n", " 3 1.2197350762795174\n", " 4 -0.368382650780104\n", " 5 -1.1826441481568593\n", " 6 -0.6403181148323545\n", " 7 0.5587052763598461\n", " 8 1.017223864540614\n", " 9 -0.36537819367057417\n", " 10 -0.9871471977883635\n", " 11 -0.08219496724163275\n", " 12 0.8424539378871415\n", " 13 1.4354830571733637\n", " 14 -0.06748007023818882\n", " 15 1.306299824133438\n", " 16 0.5966910440823265\n", " ... ...\n", ", @v2=\n", "#\n", " nil\n", " 0 1.4822302417238689\n", " 1 -0.2681759396405098\n", " 2 -0.43910781706120444\n", " 3 2.0111800756412306\n", " 4 0.4967061439338626\n", " 5 -0.8600322651416693\n", " 6 -0.15794909504720955\n", " 7 1.3078327485345884\n", " 8 1.4620962374655873\n", " 9 0.3453999027655498\n", " 10 -0.602437381054453\n", " 11 0.025981216920037048\n", " 12 1.619036581425449\n", " 13 1.9102697837552345\n", " 14 0.3215876652606383\n", " 15 1.7732661549386022\n", " 16 0.7694502443827269\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=-1.7851034060403639, @maximum_x=2.2048098062058568, @minimum_y=-1.6138177653853, @maximum_y=3.0619892105419586, @groups=nil, @data=[{:x=>1.2029523425685384, :y=>1.4822302417238689}, {:x=>-0.7284367836633963, :y=>-0.2681759396405098}, {:x=>-0.7147373926166352, :y=>-0.43910781706120444}, {:x=>1.2197350762795174, :y=>2.0111800756412306}, {:x=>-0.368382650780104, :y=>0.4967061439338626}, {:x=>-1.1826441481568593, :y=>-0.8600322651416693}, {:x=>-0.6403181148323545, :y=>-0.15794909504720955}, {:x=>0.5587052763598461, :y=>1.3078327485345884}, {:x=>1.017223864540614, :y=>1.4620962374655873}, {:x=>-0.36537819367057417, :y=>0.3453999027655498}, {:x=>-0.9871471977883635, :y=>-0.602437381054453}, {:x=>-0.08219496724163275, :y=>0.025981216920037048}, {:x=>0.8424539378871415, :y=>1.619036581425449}, {:x=>1.4354830571733637, :y=>1.9102697837552345}, {:x=>-0.06748007023818882, :y=>0.3215876652606383}, {:x=>1.306299824133438, :y=>1.7732661549386022}, {:x=>0.5966910440823265, :y=>0.7694502443827269}, {:x=>2.0385294700065892, :y=>2.4311352677698412}, {:x=>-1.214818677863248, :y=>-0.4348899575064713}, {:x=>0.5870311027486018, :y=>1.1778758340534488}, {:x=>-1.022429677768277, :y=>-0.7230749280782236}, {:x=>-0.5586261945171441, :y=>0.19611331156230105}, {:x=>-1.3266690939911527, :y=>-1.0906958344704414}, {:x=>0.5366584839864115, :y=>1.2650415073106027}, {:x=>1.2789550262639169, :y=>1.4423211783199417}, {:x=>-0.607473502658914, :y=>0.1218652017622548}, {:x=>-0.3497755699897639, :y=>0.15589556835839413}, {:x=>-0.6458494097232634, :y=>-0.3558715157773942}, {:x=>-1.7851034060403639, :y=>-1.6138177653853}, {:x=>0.6451966101574491, :y=>1.1131279575791693}, {:x=>-1.1884007303795256, :y=>-0.6323787941120471}, {:x=>0.8868753937709651, :y=>1.3071683767930022}, {:x=>-0.3647039697362235, :y=>-0.15967445029543248}, {:x=>0.24626575852014385, :y=>1.0418253631035628}, {:x=>-0.764165716261582, :y=>-0.34393535268453806}, {:x=>1.7765217401008904, :y=>2.4148679123807604}, {:x=>0.38266771946397854, :y=>0.7651428532142893}, {:x=>-0.38303858958268355, :y=>-0.20702699692984589}, {:x=>-0.3660977775714984, :y=>0.459242974503143}, {:x=>-0.8519643718807273, :y=>-0.4271064607216114}, {:x=>0.9726412934541395, :y=>1.2199869306857491}, {:x=>-0.7960450756937868, :y=>-0.25376628797959466}, {:x=>0.14091571246855109, :y=>0.5497298912317738}, {:x=>2.2048098062058568, :y=>2.733852154427126}, {:x=>0.924708758520309, :y=>1.9906789360923356}, {:x=>0.5620493830617201, :y=>1.017957468713064}, {:x=>0.9877979762866108, :y=>1.5777705602792271}, {:x=>2.050522103825718, :y=>3.0619892105419586}, {:x=>0.3430652357893582, :y=>0.7963699135135573}, {:x=>1.4783307211326213, :y=>1.9374763941125042}, {:x=>0.6898585295770393, :y=>1.2775616543425978}, {:x=>-1.211197393183886, :y=>-0.7491248924998365}, {:x=>-0.7808746006801057, :y=>-0.2796001875544937}, {:x=>0.6409553970951195, :y=>1.0561112938591308}, {:x=>0.5186184821619029, :y=>0.8280613145493414}, {:x=>1.6200023400143677, :y=>2.1345815928529612}, {:x=>-0.1652487720290011, :y=>0.4223954240383939}, {:x=>-1.0036009083579267, :y=>-0.39891166159095526}, {:x=>-0.5320441859050812, :y=>0.17202208284275666}, {:x=>-0.19079404488970378, :y=>0.5895264514475019}, {:x=>0.2353605448645314, :y=>0.6390025042207848}, {:x=>0.18630908206128774, :y=>0.6786008282724998}, {:x=>1.1443169481868662, :y=>1.4626130532512331}, {:x=>-1.2914124881033502, :y=>-1.1461009906633588}, {:x=>1.8104114107529872, :y=>1.9797232086453909}, {:x=>-0.2548352246985767, :y=>0.2427973615785814}, {:x=>-0.5757415690357025, :y=>-0.19800754369822404}, {:x=>0.15900729488260543, :y=>0.473946296340899}, {:x=>0.274683576641675, :y=>0.7492641131326262}, {:x=>-0.3537084010407285, :y=>0.3692525926095936}, {:x=>-0.10698502437645677, :y=>0.38987236975947204}, {:x=>0.7303105930966723, :y=>1.112311547394341}, {:x=>0.4114922389149686, :y=>1.0766976896031695}, {:x=>0.8414922931355174, :y=>1.4874839170727165}, {:x=>0.0444600704594401, :y=>0.4579107867345499}, {:x=>-0.23575208147949764, :y=>0.3050593249716955}, {:x=>-0.3926160167205071, :y=>0.5601728646661435}, {:x=>-1.1909369872589841, :y=>-0.4486594685529218}, {:x=>-1.6255345937416097, :y=>-1.0231735354912714}, {:x=>0.423856440718669, :y=>0.7499946187104362}, {:x=>0.15126312350071183, :y=>0.6060277891107635}, {:x=>0.9927877468213919, :y=>1.6071208208873022}, {:x=>-0.6952434717682168, :y=>-0.17829293275569824}, {:x=>-1.5286192509334864, :y=>-0.9934489369355929}, {:x=>-1.1635368687705685, :y=>-0.8716095693761595}, {:x=>-0.07275197820764326, :y=>0.375649785533018}, {:x=>0.34369031095430247, :y=>0.832215460310348}, {:x=>0.9937891351015558, :y=>1.8783704018443284}, {:x=>-0.5580852837611813, :y=>-0.15928006452959786}, {:x=>-0.25320429170833225, :y=>0.09901778127260508}, {:x=>0.8620237949412143, :y=>1.134591959544935}, {:x=>1.170260697108485, :y=>1.391219122249563}, {:x=>-1.35721199621495, :y=>-0.6662771654215293}, {:x=>-0.6814540404758149, :y=>-0.4263128478679701}, {:x=>1.0557233948308635, :y=>1.5495775697295344}, {:x=>-0.7833581676479311, :y=>-0.19772117915672482}, {:x=>0.5970196089265004, :y=>1.0559494869649688}, {:x=>0.48047272372565925, :y=>0.993845444282945}, {:x=>0.933845988839194, :y=>1.3129324849002901}, {:x=>-1.0889682832737748, :y=>-0.5704623401985089}], @x_scale=#], @type=:number, @n=true, @f=#, @g=#, @tick_format=#>, @y_scale=#], @type=:number, @n=true, @f=#, @g=#, @tick_format=#>>" ] }, "execution_count": 17, "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 }